# Bzoj1260 [cqoi2007]涂色paint

Posted by yjjr's blog on February 6, 2018

Description

Input

Output

Sample Input

Sample Output

【样例输入1】

AAAAA

【样例输入1】

RGBGR

【样例输出1】

1

【样例输出1】

3

HINT

40%的数据满足：1<=n<=10
100%的数据满足：1<=n<=50

F[l][r]=min(f[l+1][r],f[l][r-1],f[l+1][r-1]+1)  (f[l]=f[r])

F[l][r]=min{f[l][i]+f[i+1][r]}  l<=i<r

Code

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define LL long long
#define mem(x,num) memset(x,num,sizeof x)
using namespace std;
{
int f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int inf=0x3f3f3f;
int f[600][600],n;
char s[600];
int main()
{
mem(f,inf);
scanf("%s",s+1);
n=strlen(s+1);
rep(i,1,n)f[i][i]=1;
rep(l,1,n-1)
rep(i,1,n){
int j=i+l;
if(j>n)break;
if(s[i]==s[j]){
if(l==1)f[i][j]=1;
else f[i][j]=min(min(f[i+1][j],f[i][j-1]),f[i+1][j-1]+1);
}
else rep(k,i,j-1) f[i][j]=min(f[i][k]+f[k+1][j],f[i][j]);
}
printf("%d\n",f[1][n]);
return 0;
}