题目:
分析:紫书上做过,但完全不会了!
代码:
#include<bits/stdc++.h>
using namespace std;
int m;
int A[505];
int D[505][505];
int f(int x,int y)
{
if(x==y) return 1;
if(x+1==y)
{
if(A[x]==A[y]) return 1;
return 2;
}
if(D[x][y]!=-1) return D[x][y];
if(A[x]==A[y])
{
D[x][y]=f(x+1,y-1);
}
else{
D[x][y]=1<<30;
for(int i=x;i<y;i++)
D[x][y]=min(D[x][y],f(x,i)+f(i+1,y));
}
return D[x][y];
}
int main()
{
cin>>m;
for(int i=0;i<m;i++) cin>>A[i];
memset(D,-1,sizeof(D));
cout<<f(0,m-1);
}