# String painter

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2171    Accepted Submission(s): 956

Problem Description
There are two strings A and B with equal length. Both strings are made up of lower case letters. Now you have a powerful string painter. With the help of the painter, you can change a segment of characters of a string to any other character you want. That is, after using the painter, the segment is made up of only one kind of character. Now your task is to change A to B using string painter. What’s the minimum number of operations?

Input
Input contains multiple cases. Each case consists of two lines:
The first line contains string A.
The second line contains string B.
The length of both strings will not be greater than 100.

Output
A single line contains one integer representing the answer.

Sample Input
zzzzzfzzzzz abcdefedcba abababababab cdcdcdcdcdcd

Sample Output
6 7

Source

Recommend

/*

然后ans[i]记录第一个串前i个字符全部涂成b钱i个字符的步数
那么当来了b 一个字符 如果相等那么ana[i]=min(dp[0][i],ans[i-1])
然后还可能是前面任意一个涂好的ans[j] 再见过dp[j+1][i]而来

*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

using namespace std;

#define N 105

int dp[N][N],s[N];
char a[N],b[N];

int main()
{
int i,j;
while(~scanf("%s",a))
{
scanf("%s",b);
memset(dp,0,sizeof(dp));
int len=strlen(b);

for(i=0;i<len;i++)
dp[i][i]=1;

for(i=len-1;i>=0;i--)
for(j=i+1;j<len;j++)
{
dp[i][j]=dp[i+1][j]+1;

for(int k=i+1;k<=j;k++)
if(b[i]==b[k])
{
dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
}

}

for(i=0;i<len;i++)
{
s[i]=dp[0][i];
if(a[i]==b[i])
{
if(i==0)
s[i]=0;
else
s[i]=s[i-1];
}
else
for(int k=0;k<i;k++)
s[i]=min(s[i],s[k]+dp[k+1][i]);
}
printf("%d\n",s[len-1]);
}
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：HDU 2476 String painter（区间dp） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)