标签:
40%的数据满足:1<=n<=10
100%的数据满足:1<=n<=50
可以令f[i][j]表示涂i->j的最少次数。初始f[i][j]=+∞,f[i][i]=1。
若i=j,则f[i][j]=min(f[i+1][j],f[i][j-1],f[i+1][j-1]+1)。i!=j f[i][j]=min(f[i][k]+f[k+1][j])
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<cmath> #include<vector> using namespace std; char s[55]; int len,j,f[55][55]; int main() { scanf("%s",s+1); len=strlen(s+1); memset(f,127,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<len;i++) { j=i+l; if (j>len) break; if (s[i]==s[j]) { f[i][j]=min(f[i+1][j],f[i][j-1]); f[i][j]=min(f[i][j],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",f[1][len]); return 0; }
标签:
原文地址:http://www.cnblogs.com/ws-fqk/p/4645523.html