题解:
水题,区间dp。
代码如下:
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
const int maxn=55;
int n,a[maxn],f[maxn][maxn];
char s[maxn];
int main(){
scanf("%s",s); n=strlen(s);
for (int i=0;i<=n;i++) a[i+1]=s[i]-'A'+1;
memset(f,63,sizeof(f));
for (int i=1;i<=n;i++) f[i][i]=1;
for (int l=2;l<=n;l++)
for (int i=1;i<=n-l+1;i++){
int j=i+l-1;
if (a[i]==a[j]) f[i][j]=min(f[i+1][j],min(f[i][j-1],f[i+1][j-1]+1));
else for (int k=i;k<j;k++) f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]);
}
printf("%d\n",f[1][n]);
return 0;
}