hdu 2476 String painter 区间dp 难
2016-04-02 19:07
423 查看
一种比较笨的方法,人家都开的是二维数组,我却开了三维。
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2862 Accepted Submission(s): 1307
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
2008 Asia Regional Chengdu
Recommend
lcy | We have carefully selected several similar problems for you: 2480 2481 2478 2482 2475
Statistic | Submit | Discuss | Note
String painter
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2862 Accepted Submission(s): 1307
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
2008 Asia Regional Chengdu
Recommend
lcy | We have carefully selected several similar problems for you: 2480 2481 2478 2482 2475
Statistic | Submit | Discuss | Note
/**========================================== * This is a solution for ACM/ICPC problem * * @source:2008 Asia Regional Chengdu String painter * @type: 区间dp * @author: wust_ysk * @blog: http://blog.csdn.net/yskyskyer123 * @email: 2530094312@qq.com *===========================================*/ #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> using namespace std; typedef long long ll; const int INF =0x3f3f3f3f; const int maxn= 100 ; char a[maxn+10]; char b[maxn+10]; int dp[27][maxn+10][maxn+10]; int n,pre[maxn+5],nex[maxn+5]; void cope() { pre[1]=0; for(int i=2;i<=n;i++) { pre[i]= b[i]==b[i-1]? pre[i-1]:i-1 ; } nex =n+1; for(int i=n-1;i>=1;i--) { nex[i]= b[i]==b[i+1]?nex[i+1]:i+1; } } void work() { for(int i=1;i<=n;i++) { dp[0][i][i]=a[i]==b[i]? 0: 1 ; for(int j=1;j<=26;j++) { dp[j][i][i]= 'a'+j-1==b[i]?0:1; } } for(int add=1;add<n;add++) { for(int le=1;le+add<=n;le++) { int ri=le+add; for(int j=0;j<=26;j++) { dp[j][le][ri]=ri-le+1; for(int k=le;k<ri;k++) { dp[j][le][ri]=min(dp[j][le][ri],dp[j][le][k]+dp[j][k+1][ri]); } if(b[le]!=b[ri]) continue; int st=nex[le],ed=pre[ri]; int x=b[le]-'a'+1; if(st>ed) dp[j][le][ri]=min(dp[j][le][ri],1); else dp[j][le][ri]=min(dp[j][le][ri], 1+dp[x][st][ed]); } } } printf("%d\n",dp[0][1] ); } int main() { while(~scanf("%s",a+1)) { scanf("%s",b+1); n=strlen(a+1); cope(); work(); } return 0; }
相关文章推荐
- tail -f -F是个好东西
- idea修改文件名后出现main method should be static错误
- Design Pattern Explained 读书笔记二——设计模式序言
- 【POJ1765】November Rain——扫描线+线段树
- LeetCode. Container With Most Water
- IBM openblockchain学习(三)--Ledger源码分析
- 【LeetCode】220. Contains Duplicate III
- error: command 'gcc' failed with exit status 1 的解决办法
- RAID5工作原理介绍
- 219. Contains Duplicate II
- LeetCode 336. Palindrome Pairs
- DrawableContainer
- lintcode-medium-Number of Airplanes in the Sky
- 使用MacBook Air的4项基本技巧
- Xenomai on the Beaglebone Black in 14 easy steps ---fwqlzz love is for ever
- 11. Container With Most Water
- 阶梯博弈(Stair Nim)
- leetcode 172. Factorial Trailing Zeroes
- rails中使用ajax
- 领域驱动设计(Domain Driven Design)参考架构详解