bzoj1260[CQOI2007]涂色paint
思路:区间dp,用f[i][j]表示区间[i,j]的答案,然后转移即可。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 8 char s[100]; 9 int f[100][100]; 10 11 int main(){ 12 scanf("%s",s+1);int n=strlen(s+1); 13 for (int i=1;i<=n;i++) f[i][i]=1; 14 for (int len=2;len<=n;len++){ 15 for (int l=1;l<n;l++){ 16 int r=l+len-1;if (r>n) break; 17 f[l][r]=min(f[l][r-1]+!(s[r]==s[r-1]),f[l+1][r]+!(s[l]==s[l+1])); 18 if (s[l]==s[r]) f[l][r]=min(f[l][r],min(f[l+1][r],f[l][r-1])); 19 if (l+1<=r-1) 20 if (s[l]==s[r]) f[l][r]=min(f[l+1][r-1]+1,f[l][r]); 21 if (s[l]!=s[r]) for (int k=l; k<r; k++) f[l][r]=min(f[l][r],f[l][k]+f[k+1][r]); 22 } 23 } 24 printf("%d\n",f[1][n]); 25 return 0; 26 }