hdu 2476 刷墙

1、http://acm.hdu.edu.cn/showproblem.php?pid=2476

2、题目大意:

有两个字符串长度相同,现在有一个painter,一次可以把第一个字符串中的一段区间内的所有字母都换成同一个字母(这个字母可以是任意一个),问最少执行多少次操作,才能将第一个字符串转换成第二个字符串

3、解题思路:

dp[i][j]表示i-j区间内的最少次数

先操作第二个字符串,我们先假设第i个字符的位置需要喷刷一次,对于所有的dp[i][j]=dp[i+1][j]+1、

在i-j区间内,如果有第k个跟第i个相同,那么就可以将i-j区间借助k分成两部分dp[i][j]=min(dp[i+1][k]+dp[k+1][j]);

处理完第2个字符串后,我们就要看第一个字符串究竟需要喷刷多少次了,用ans[i]记录0-i区间第二个字符串得出的喷刷次数,如果第一个字符串的i位置与第二个字符串的i位置相同,那么这个位置就不用喷刷了,ans[i]=ans[i-1],如果不相同,就要就要借助一个位于o-i区间内的变量来分割开

4、题目:

String painter

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


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
lcy   |   We have carefully selected several similar problems for you:   2480  2481  2478  2482  2475
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;
#define LL long long
#define me(x,y) memset(x,y,sizeof(x));
#define bug printf("***********\n")
using namespace std;
const int mod=1e9+7;
const int maxn=1e4+7;
const int maxx=1e3+100;
const int INF=1<<30;

int n,dp[maxx][maxx],i,j,k,l;

int cas=1,ans[maxx];
int main()
{
   // freopen("in.txt", "r", stdin);
    string s1,s2;
    while(cin>>s1>>s2)
    {
        me(dp,0);
        for(int j=0;j<s1.size();j++)
        {
            for(int i=j;i>=0;i--)
            {
                dp[i][j]=dp[i+1][j]+1;
                for(int k=i+1;k<=j;k++)
                {
                    if(s2[i]==s2[k])
                        dp[i][j] = min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
                }
            }
        }
        for(int j=0;j<s1.size();j++)
            ans[j]=dp[0][j];
        for(int j=0;j<s1.size();j++)
        {
            if(s1[j]==s2[j])
                ans[j]=ans[j-1];
            else
                for(int k=0;k<j;k++)
                    ans[j]=min(ans[j],ans[k]+dp[k+1][j]);
        }
       // printf("Case #%d: %d\n",cas++,dp[1][n]);
        cout<<ans[s1.size()-1]<<endl;
    }

    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值