题意

String painter

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


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 
 

Statistic | Submit | Discuss | Note

分析

参照AndyQsmart的题解。

读完题首先会想到的自然是用区间dp,但是列出来发现,没办法区间合并。因为一旦需要考虑对某一段成段染色的话,在区间合并的时候,就无法考虑转移过程中起始串的变化了。

既然这样,就不考虑成段染色造成的影响了,就当起始串和目标串处处不想等。

那么考虑区间[i, i+len],

自然遍历子区间[i, j],

如果[i, j]和[j+1, i+len]需要合并的话,

如果考虑成段染色的话,只有str2[i] == str2[j+1]时,考虑成段染色[i, j+1],但是[i, j+1]的父区间又有可能会成段然和str2[i]一样的颜色,所以不能直接将区间缩短成[i+1, j]和[j+2, i+len],所以可以考虑这一步的效果只相当于染str2[j+1]的时候,可以少染一个str2[i]。那么区间就变成[i+1, j]和[j+1, i+len], 这样父区间中可能再次出现一个i`,和j+1产生成段染色,即

p[i][i+len] = min(p[i][i+len], p[i+1][j]+p[j+1][i+len]);

然后就是考虑使用p来计算ans[i],表示前i个字符从起始串到目标串的步数。

ans[0]自然好考虑,只需要判断一下str1[0]和str2[0]。

对于ans[i],

如果str1[i] == str2[i],自然就可以退化成ans[i-1]。

其它情况,自然是遍历子区间ans[j]和p[j+1][i]进行合并。

时间复杂度\(O(n^3)\)

代码

#include<iostream>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;
    rg char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(isdigit(ch))
        data=data*10+ch-'0',ch=getchar();
    return data*w;
}
template<class T>il T read(rg T&x){
    return x=read<T>();
}
typedef long long ll;

char str1[105],str2[105];
int n,p[105][105],ans[105];
void work(){
    for(int i=0;i<n;++i) p[i][i]=1;
    for(int len=1;len<n;++len)
        for(int i=0;i<n&&i+len<n;++i){
            p[i][i+len]=p[i+1][i+len]+1;
            for(int j=i;j<i+len;++j)if(str2[i]==str2[j+1])
                p[i][i+len]=std::min(p[i][i+len],p[i+1][j]+p[j+1][i+len]);
        }
    ans[0]=str1[0]==str2[0]?0:1;
    for(int i=1;i<n;++i){
        ans[i]=str1[i]==str2[i]?ans[i-1]:p[0][i];
        for(int j=0;j<i;++j)
            ans[i]=std::min(ans[i],ans[j]+p[j+1][i]);
    }
    printf("%d\n",ans[n-1]);
}
int main(){
//  freopen(".in","r",stdin),freopen(".out","w",stdout);
    while(~scanf("%s%s",str1,str2)){
        n=strlen(str1);
        work();
    }
    return 0;
}

HDU2476 String painter的更多相关文章

  1. HDU2476 String painter —— 区间DP

    题目链接:https://vjudge.net/problem/HDU-2476 String painter Time Limit: 5000/2000 MS (Java/Others)    Me ...

  2. hdu2476 String painter(区间dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2476 Problem Description There are two strings ...

  3. HDU2476 String painter(DP)

    题目 String painter 给出两个字符串s1,s2.对于每次操作可以将 s1 串中的任意一个子段变成另一个字符.问最少需要多少步操作能将s1串变为s2串. 解析 太妙了这个题,mark一下. ...

  4. HDU2476 String painter——区间DP

    题意:要把a串变成b串,每操作一次,可以使a串的[l,r]区间变为相同的一个字符.问把a变成b最少操作几次. 这题写法明显是区间dp ,关键是处理的方法. dp[l][r]表示b串的l~r区段至少需要 ...

  5. ACM学习历程—HDU2476 String painter(动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意是给定一个起始串和一个目标串,然后每次可以将某一段区间染成一种字符,问从起始串到目标串最少需要染多 ...

  6. 刷题总结——String painter(hdu2476)

    题目: Problem Description There are two strings A and B with equal length. Both strings are made up of ...

  7. HDOJ 题目2474 String painter(区间DP)

    String painter Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. uva live 4394 String painter 间隔dp

    // uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...

  9. HDU 2476 String painter(区间DP)

    String painter Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

随机推荐

  1. grub 启动错误 "file not found"

    刚安装ubuntu 14.4 竟然出现这么尴尬的事,ubuntu 行不行? 幸好还是能从u盘启动后,转到硬盘. 然后在网上找到了解决方法. http://askubuntu.com/questions ...

  2. 在浏览器外打开PDF文档

    One function you might find annoying with PDFs and PDF Readers is that when you click on a link to a ...

  3. docker on mac:误删default vm的处理方法

    通过docker-machine 重新建一个vm,在virtualbox里可以直接看到多出来一个vm docker-machine create --driver virtualbox default

  4. Python递归遍历《指定目录》下的所有《文件》

    https://www.cnblogs.com/dreamer-fish/p/3820625.html

  5. WPF样式——经典博客

    WPF样式博客:http://www.cnblogs.com/luluping/archive/2011/05/06/2039498.html

  6. js下载图片

    DownloadImgZP = imgPath => { const image = new Image(); // 解决跨域 Canvas 污染问题 image.setAttribute('c ...

  7. Connected Components? CodeForces - 920E (bfs)

    大意:给定无向图, 求补图的连通块数 bfs模拟即可, 这里用了map存图, set维护未划分的点集, 复杂度$O(nlog^2n)$, 用链表的话可以$O(n)$ #include <iost ...

  8. inherit

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace { cl ...

  9. EchoService

    dubbo为consumer端的代理对象实现了EchoService接口. 使用示例: <dubbo:reference id="hello" interface=" ...

  10. cas AuthenticationFilter

    AuthenticationFilter *** 这个类的作用:判断是否已经登录,如果没有登录则根据配置的信息来决定将跳转到什么地方 *** casServerLoginUrl:定义cas 服务器的登 ...