Bzoj 1260: [CQOI2007]涂色paint (区间DP)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1260
囤了不少题,有人问为什么不更博客了,
算了,更一发吧(这算不算失踪人口回归??)
(f[i][j])表示(i~j)被染色的最小次数.
反过来考虑,先把最后的几个操作放在前面.然后进行区间DP.即可
分为三种情况考虑:
(1.l == r f[l][r] = 1;)
(2.s[l] == s[r] f[l][r] = min(f[i + 1][r] , f[l][r - 1]))
3.不满足上述条件的话.
枚举断点.
#include <iostream>
#include <cstring>
#include <cstring>
#include <cstdio>
#define rep(i,x,p) for(register int i = x;i <= p;++ i)
#define sep(i,x,p) for(register int i = x;i >= p;-- i)
const int maxN = 50 + 7;
using namespace std;
char s[maxN];
int f[maxN][maxN];
int main() {
scanf("%s",s + 1);
int n = strlen(s + 1);
memset(f,0x3f,sizeof(f));
rep(i,1,n) f[i][i] = 1;
rep(len,1,n - 1) {
for(register int l = 1;l + len <= n;++ l){
int r = l + len;
if(s[l] == s[r]) f[l][r] = min(f[l + 1][r] , f[l][r - 1]);
rep(k , l ,r - 1) f[l][r] = min(f[l][k] + f[k + 1][r],f[l][r]);
}
}
printf("%d", f[1][n]);
return 0;
}