P4170 [CQOI2007]涂色
#include<bits/stdc++.h> using namespace std; const int n=55; char s[n]; int f[n][n]; //const int INF=1e8; int main(){ scanf("%s",s+1); int len = strlen(s+1); memset(f,0x3F,sizeof(f)); for(int i=1;i<=len;i++){ f[i][i]=1; } for(int l=1;l<len;l++){ for(int i=1;i+l<=len;i++){ int j=i+l; if(s[i]==s[j]){ f[i][j]=min(f[i+1][j],f[i][j-1]); } else { for(int k=i;k<=j-1;k++){ f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]); } } } } printf("%d\n",f[1][len]); return 0; }