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
题意:给定两个字符串a和b,求最少须要对a进行多少次操作,才干将a变成b。每次操作时将a中随意一段变成随意一个字母所组成的段。
#include<stdio.h>
#include<string.h>
int min(int a,int b)
{
return a>b? b:a;
}
int main()
{
int dp[105][105],ans[105];
char a[105],b[105];
while(scanf("%s%s",a,b)>0)
{
int len=strlen(a);
memset(dp,0,sizeof(dp));
for(int r=0;r<len;r++)
for(int i=0;i<len-r;i++)
{
int j=i+r;
dp[i][j]=dp[i+1][j]+1;
for(int k=i+1;k<=j;k++)
if(b[i]==b[k])
dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
}
for(int i=0;i<len; i++)
ans[i]=dp[0][i];
for(int i=0;i<len; i++)
if(a[i]==b[i])
{
if(i==0)ans[i]=0;
else ans[i]=ans[i-1];
}
else{
for(int k=0;k<i;k++)
ans[i]=min(ans[i],ans[k]+dp[k+1][i]);
}
printf("%d\n",ans[len-1]);
}
}

hdu2476String painter (区间DP)的更多相关文章

  1. hdu2476 String painter(区间dp)

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

  2. hdu_2476_String painter(区间DP)

    题目链接:hdu_2476_String painter 题意: 有a,b两字符串,现在你有一个刷子,每次可以任选一个区间,使这个区间变成你想要的字符,现在让你将a变成b,问最少刷多少次 题解: 考虑 ...

  3. HDU 2476 String painter(区间dp)

    题意: 给定两个字符串,让求最少的变化次数从第一个串变到第二个串 思路: 区间dp, 直接考虑两个串的话太困难,就只考虑第二个串,求从空白串变到第二个串的最小次数,dp[i][j] 表示i->j ...

  4. HDU2476 String painter——区间DP

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

  5. HDU2476 String painter —— 区间DP

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

  6. uva live 4394 String painter 区间dp

    // uva live 4394 String painter // // 这一题是训练指南上dp专题的习题,初看之下认为仅仅是稍微复杂了一点 // 就敲阿敲阿敲,两个半小时后,发现例子过了.然而自己 ...

  7. hdu 2476"String painter"(区间DP)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给定字符串A,B,每次操作可以将字符串A中区间[ i , j ]的字符变为ch, ...

  8. HDU 2476 String painter(区间DP+思维)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意:给你字符串A.B,每次操作可以将一段区间刷成任意字符,问最少需要几次操作可以使得字符串 ...

  9. 区间DP小结

    也写了好几天的区间DP了,这里稍微总结一下(感觉还是不怎么会啊!). 但是多多少少也有了点感悟: 一.在有了一点思路之后,一定要先确定好dp数组的含义,不要模糊不清地就去写状态转移方程. 二.还么想好 ...

随机推荐

  1. [Java] 总结1.5/1.6/1.7版本的特性

    开发过程中接触到了从jdk1.5---jdk1.7的使用,在不同的阶段,都使用过了jdk的一些新特性,操作起来更加方面啦!特此总结了下,以下是测试代码: JDK1.5新特性: 1.自动装箱与拆箱: I ...

  2. [转]C# ListView 单击标题实现排序(在转载的基础上有所完善)

    using System; using System.Collections; using System.Windows.Forms; //在转载的基础上有所完善 namespace TDRFacto ...

  3. 外观模式(Facade)C++实现

    外观模式 意图: 为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一系统更加容易使用. 适用性: 1.在设计初期阶段,应该要有意识的将不同的两个层分离,比如经典的三层架 ...

  4. 开源作品-PHP写的JS和CSS文件压缩利器(单文件绿色版)-SuMinify_PHP_1_5

    前言: 网站项目需要引用外部文件以减小加载流量,而且第一次加载外部资源文件后,其他同域名的页面如果引用相同的地址,可以利用浏览器缓存直接读取本地缓存资源文件,而不需要每个页面都下载相同的外部资源文件. ...

  5. how does Array.prototype.slice.call() work?

    763 down vote accepted +50 What happens under the hood is that when .slice() is called normally, thi ...

  6. Windows2012R2 时间同步设置

    Windows2012R2里没有了internet时间,或者Internet时间无法同步成功,都可以尝试使用如下方法. 1.打开命令提示符, 输入:gpedit.msc,打开组策略管理器 2.执行上述 ...

  7. IAAS: IT公司去IOE-Alibaba系统构架解读

    从Hadoop到自主研发,技术解读阿里去IOE后的系统架构 原地址:...................... 云计算阿里飞天 摘要:从IOE时代,到Hadoop与飞天并行,再到飞天单集群5000节 ...

  8. spring cloud(三) config

    spring cloud 配置中心 config 搭建过程 1.搭建config-server 服务端 1.1. 新建boot工程 pom引入依赖 <!-- config配置中心 --> ...

  9. React从安装到实战

    建议:初学者看之前请先看一遍菜鸟教程 可以安装一个ATOM编辑器,本人觉得很好用 一.安装并启动项目:网址  搭建好的项目目录为: 二.开始写项目: 1.组件到界面流程: 定义一个组件app.js导出 ...

  10. python简单的输入与输出

    1 首先利用python完成简单的输出,运行如下: python和c语言类似,但又有所不同,python开发快,语言简洁,我是这样对比学的 输出:print+空格+'要输出的内容',一定要是英文状态下 ...