再一道区间DP -- P4170 [CQOI2007]涂色
https://www.luogu.org/problemnew/show/P4170
一道简单的区间DP,注意读入
#include <bits/stdc++.h> #define up(i,l,r) for(int i = (l);i <= (r); i++) using namespace std; int f[55][55]; char s[55]; int main() { // freopen("color.in","r",stdin); memset(f,0x7F,sizeof(f)); scanf("%s",s + 1); int n = strlen(s + 1); up(i,1,n) f[i][i] = 1; up(L,1,n - 1) for(int i = 1,j = i + L; j <= n;i++,j++) { if(s[i] == s[j]) f[i][j] = min(f[i + 1][j],f[i][j - 1]); else up(k,i,j - 1) f[i][j] = min(f[i][j],f[i][k] + f[k + 1][j]); } printf("%d",f[1][n]); return 0; }