博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 2476 String Painter
阅读量:6710 次
发布时间:2019-06-25

本文共 1917 字,大约阅读时间需要 6 分钟。

 

 

String painter

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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

 

 

比较好的区间dp,可以先求空串到目标串的最少操作数

然后再对原串进行dp

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 using namespace std; 9 const int MAXN=110;10 #define For(i,n) for(int i=1;i<=n;i++)11 #define Rep(i,l,r) for(int i=l;i<=r;i++)12 #define Down(i,r,l) for(int i=r;i>=l;i--)13 int dp[MAXN][MAXN];14 char str1[MAXN],str2[MAXN];15 int ans[MAXN];16 int main(){17 while(~scanf("%s%s",str1+1,str2+1)){18 int n=strlen(str1+1);19 memset(dp,0,sizeof(dp));20 For(i,n)21 Rep(j,i,n) dp[i][j]=j-i+1;22 Down(i,n-1,1)23 Rep(j,i+1,n){24 dp[i][j]=dp[i+1][j]+1;25 Rep(k,i+1,j)26 if(str2[i]==str2[k])27 dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j]);28 }29 For(i,n){30 ans[i]=dp[1][i];31 if(str1[i]==str2[i]) ans[i]=ans[i-1];32 For(j,i) ans[i]=min(ans[i],ans[j]+dp[j+1][i]);33 }34 printf("%d\n",ans[n]);35 }36 return 0;37 }
Codes

 

转载于:https://www.cnblogs.com/zjdx1998/p/4057641.html

你可能感兴趣的文章
[转]请记住别拿自己跟别人比较
查看>>
srl16e verilog
查看>>
硬笔书法练习
查看>>
带您走进七周七语言的程序世界
查看>>
Stream和Reader、Writer对象学习
查看>>
Boost简介
查看>>
计算机常用端口一览表:
查看>>
Android 自带图标库 android.R.drawable
查看>>
HTTP Error: status code 302. I/O Error: Error #2038. ckfinder报错
查看>>
java.util.Date和java.sql.Date的区别和相互转化
查看>>
selenium面试题总结
查看>>
本次孩子流感总结
查看>>
Baby Ming and Matrix games(dfs计算表达式)
查看>>
eclipse代码提示框背景色改动
查看>>
April Fools Day Contest 2016 G. You're a Professional
查看>>
在Ubuntu的系统中怎样将应用程序加入到開始菜单中
查看>>
SDL绑定播放窗口 及 视频窗口缩放
查看>>
BIO与NIO、AIO的区别(这个容易理解)
查看>>
springboot+mybatis+ehcache实现缓存数据
查看>>
MUI 选项卡切换+下拉刷新动态 完整实现一例
查看>>