Link:http://acm.hdu.edu.cn/showproblem.php?pid=2476
String painter
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2543 Accepted Submission(s): 1145
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.
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
AC code:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<vector>
#include<string.h>
#define LL long long
using namespace std;
const int INF=0x3f3f3f3f;
int dp2[111][111];
int dp[111];
char s1[111],s2[111];
int main()
{
//freopen("D:\\in.txt","r",stdin);
int T,cas,i,j,k,n,m,len;
while(scanf("%s%s",s1+1,s2+1)!=EOF)
{
len=strlen(s2+1);
memset(dp2,0,sizeof(dp2));
memset(dp,0,sizeof(dp));
for(i=1;i<=len;i++)
dp2[i][i]=1;
for(i=len-1;i>=1;i--)
{
for(j=i+1;j<=len;j++)
{
//dp2[i][j]=min(dp2[i+1][j]+(s2[i]==s2[i+1]?0:1),dp2[i+1][j-1]+(s2[j]==s2[j-1]?0:1));
dp2[i][j]=dp2[i+1][j]+1;
for(k=i+1;k<=j;k++)
{
if(s2[i]==s2[k])
dp2[i][j]=min(dp2[i][j],dp2[i+1][k-1]+dp2[k][j]);
}
}
}
for(i=1;i<=len;i++)
{
dp[i]=dp2[1][i];
if(s1[i]==s2[i])
{
if(i==1)
dp[i]=0;
else
dp[i]=dp[i-1];
}
else
{
for(j=1;j<i;j++)
{
dp[i]=min(dp[i],dp[j]+dp2[j+1][i]);
}
}
}
printf("%d\n",dp[len]);
}
return 0;
}