题意

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. Silverlight自定义控件系列 – TreeView (2) 基本布局和States

    TreeView的树形结构都以缩进方式显示,现在来完成这部分. 首先,要定义出每个节点上都包含什么东西.先看看Win7资源管理器的TreeView: 图2.1 资源管理器 一个通用的TreeView至 ...

  2. 20170622xlVBA多部门分类汇总同类合并单元格

    Public Sub Basic_CodeFrame() AppSettings On Error GoTo ErrHandler Dim StartTime, UsedTime As Variant ...

  3. 49 BOM 和DOM

    一.BOM window 对象  1.确认,输入,    window.alert(123) // 弹框    let ret = window.confirm("是否删除") / ...

  4. Dubbo 环境搭建

    CentOS7 x64 JDK1.8 1. wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.8/zoo ...

  5. python-day39--数据库

    1.什么是数据:描述事物的特征,提取对自己有用的信息  称之为数据 2..什么是数据库: 数据库即存放数据的仓库,只不过这个仓库是在计算机存储设备上,而且数据是按一定的格式存放的 为什么要用数据库: ...

  6. 24.2 网络编程基础——System.Net 命名空间

    使用C#进行网络编程时,通常要用到: System. Net  命名空间. System. Net. Sockets  命名空间. System. Net. Mail  命名空间. 24.2.1 Sy ...

  7. Mac安装fish shell

    1.brew update 2.brew install fish 3.sudo vi /etc/shells 增加内容:/usr/local/bin/fish   ##增加fish到shell环境变 ...

  8. Centos7上部署openstack ocata配置详解

    之前写过一篇<openstack mitaka 配置详解>然而最近使用发现阿里不再提供m版本的源,所以最近又开始学习ocata版本,并进行总结,写下如下文档 OpenStack ocata ...

  9. Vue--- 手动禁止ESlint

    使用vue-cli构建项目时,通常会问你要不要 “Use ESlint to lint your code?” 建议使用,这样会有助于规范我们的代码(这也是一种审美),ESlint的规范就不说了,写多 ...

  10. SQL基础用法(实例一)

    /* 2006年10月01日 SQL Server 数据库的基本操作 (1) 数据库的创建 (2) 数据表的创建以及相关约束的指定(含临时表) (3) 数据的添/删/改 (4) 数据的查询 */ () ...