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

171 篇文章 0 订阅
102 篇文章 0 订阅



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
  
  
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值