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. [HTML] 如何使用robots.txt防止搜索引擎抓取页面

    Robots.txt 文件对抓取网络的搜索引擎漫游器(称为漫游器)进行限制.这些漫游器是自动的,在它们访问网页前会查看是否存在限制其访问特定网页的 robots.txt 文件.如果你想保护网站上的某些 ...

  2. 如何让音频跟视频在ios跟android上自动播放

    如何让音频跟视频在ios跟android上自动播放 <audio autoplay ><source src="audio/alarm1.mp3" type=&q ...

  3. ps -aux ,ps aux ,ps -ef 的区别

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  4. 前端(小程序)项目Aes.js/Md5.js加密的处理方法

    做项目中需要对前端数据加密传输这个时候需要用到前端加密的算法主要是:Aes.js,Md5.js 一.Vue项目用到的aes.js加密. 1.直接在index.html引入aes.js或者在npm in ...

  5. C#解除某类警告。。。。。。。。。。

    C#预处理器指令取消不必要的警告 今天将自己写的一个类库生成一个DLL后,想把注释也加进去.... 方法:在属性->生成选项卡->XML文档文件(勾选)(生成的文件名不能修改,使用时必须跟 ...

  6. javascript中的原型对象

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. hdu3938 Portal 离线的并查集

    离线算法是将全部输入都读入,计算出所有的答案以后再输出的方法.主要是为避免重复计算.类似于计算斐波那契数列的时候用打表的方法. 题目:给一个无向图,求有多少个点对,使得两点间的路径上的花费小于L,这里 ...

  8. Kettle bug收集

    20160919(未确定): 加载表的使用"Use batch update for inserts"会引致奇怪的转换失败? 出错日志: - linenr 450000- line ...

  9. Python框架、库和软件资源大全(整理篇)

    有少量修改,请访问原始链接.PythonWIn的exe安装包;http://www.lfd.uci.edu/~gohlke/pythonlibs/ 原文链接:codecloud.net/python- ...

  10. RabbitMQ学习之ConntectionFactory与Conntection的认知

    在发送和接收消息重要的类有:ConnectionFactory, Connection,Channel和 QueueingConsumer. ConntectionFactory类是方便创建与AMQP ...