题目:http://lightoj.com/volume_showproblem.php?problem=1422
#include <cstdio> #include <iostream> using namespace std; int c[105]; int dp[105][105]; int main () { int T, N; scanf("%d", &T); for(int kase=1; kase<=T; kase++) { scanf("%d", &N); for(int i=0; i<N; i++) { scanf("%d", &c[i]); dp[i][i] = 1; } for(int step=1; step<N; step++) { for(int i=0; i+step<N; i++) { int j = i+step; dp[i][j] = dp[i][j-1]+1; for(int k=i; k<j; k++) { if(c[j]==c[k]) { dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+1][j-1]); } } } } printf("Case %d: %d\n", kase, dp[0][N-1]); } return 0; }
时间: 2024-12-30 03:51:12