http://www.cnblogs.com/kuangbin/archive/2013/04/29/3051392.html
http://www.cnblogs.com/ziyi--caolu/archive/2013/08/01/3229668.html
http://www.cfanz.cn/index.php?c=article&a=read&id=172173
#include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <cctype> #include <vector> #include <iterator> #include <set> #include <map> #include <sstream> using namespace std; #define mem(a,b) memset(a,b,sizeof(a)) #define pf printf #define sf scanf #define spf sprintf #define pb push_back #define debug printf("! ") #define INF 10000 #define MAX(a,b) a>b?a:b #define blank pf(" ") #define LL long long #define ALL(x) x.begin(),x.end() #define INS(x) inserter(x,x.begin()) #define pqueue priority_queue const int MAXN = 1000 + 5; int n,m; int dp[110][110]; int a[110]; int main() { int i,j; int t; cin>>t; while(t--) { cin>>n; for(i=1;i<=n;i++) { sf("%d",&a[i]); } for(int i = 1;i<=n;i++) { for(j=i;j<=n;j++) { dp[i][j] = j-i+1; } } for(int i = n-1;i>=1;i--) { for(j=i+1;j<=n;j++) { dp[i][j] = dp[i+1][j] + 1; for(int k = i;k<=j;k++) { if(a[i]==a[k]) dp[i][j] = min(dp[i][j],dp[i+1][k-1]+dp[k][j]); } } } pf("%d ",dp[1][n]); } return 0; } /* 2 4 1 2 1 2 7 1 2 1 1 3 2 1 */