String painter(DP综合题:区间DP(两次DP))

来源:互联网 发布:联通显示4g但没有网络 编辑:程序博客网 时间:2024/06/05 11:27



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.
 

Output
A single line contains one integer representing the answer.
 

Sample Input
zzzzzfzzzzzabcdefedcbaababababababcdcdcdcdcdcd
 

Sample Output
67
 

Source
2008 Asia Regional Chengdu
 



AC code:

#include<iostream>#include<algorithm>#include<stdio.h>#include<cstring>#include<cmath>#include<vector>#include<string.h>#define LL long longusing 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;}


0 0
原创粉丝点击