题意

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. 总是有个yumBackend.py阻止我用yum进行更新

    [Another app is currently holding the yum lock; waiting for it to exit...] 上网查了,好像是说帮我安个桌面图标的进程. 估计是 ...

  2. JDK1.5 新特性

    1:自动装箱与拆箱 自动装箱:每当需要一种类型的对象时,这种基本类型就自动地封装到与它相同类型的包装中. 自动拆箱:每当需要一个值时,被装箱对象中的值就被自动地提取出来,没必要再去调用intValue ...

  3. android--------Android内存分析工具的使用

    内存分析(in-memory analytics)是我们编写速度快.效率高的代码必不可少的知识.如果自己编写的代码在内存的分配一无所知,我想这样的程序让你去优化,应该是无从下手的.那么内存分析是什么? ...

  4. 快速读入fread

    struct FastIO { static const int S = 1e7; int wpos; char wbuf[S]; FastIO() : wpos(0) {} inline int x ...

  5. 字 字节 比特,以及各个算数类型所占用的大小范围 c++

    字 储存的基本单元,. 1字=4/8字节 字节 byte 字节是计算机中数据处理的基本单元. 1byte=8bit 比特(bit/位) 表示二进制位,计算机内部数据储存的最小单位 例如 1011是一个 ...

  6. POJ-1180 Batch Scheduling (分组求最优值+斜率优化)

    题目大意:有n个任务,已知做每件任务所需的时间,并且每件任务都对应一个系数fi.现在,要将这n个任务分成若干个连续的组,每分成一个组的代价是完成这组任务所需的总时间加上一个常数S后再乘以这个区间的系数 ...

  7. Leetcode 526

    class Solution { public: int countArrangement(int N) { vector<int> nums; ;i <= N;i++) nums. ...

  8. POJ服务器不能启动问题

    问题1: 启动tomcat时出现错误:tomcat Address already in use: JVM_Bind:80 按照网上的方法,查找占用80端口的进程:netstat -ano 任务管理器 ...

  9. windows下python安装Numpy、Scipy、matplotlib模块

    来源http://blog.csdn.net/Katrina_ALi/article/details/64922107 http://blog.csdn.net/qq_16633405/article ...

  10. Dash:程序员的好帮手(转载)

    作为一名死coder,每天最常见的动作就是查看各种API文档,你一定也有过同时打开N个窗口(HTML.PDF.CHM),不停的在编辑器与文档之间切换的感受吧?怎么说呢,其实我很讨厌这种枯燥无味的动作, ...