HDU 2476 String painter(区间dp)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 2476 String painter(区间dp)相关的知识,希望对你有一定的参考价值。
String painter
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2171 Accepted Submission(s): 956
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.
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
/* 题意:将第一个字符串转化为第二个字符串最少步数(每一步能够将一个区间变成一种颜色) 思路: 先如果两个串全然不同涂色,也就是dp[i][j]代表b串i~j全然不同涂色的最小步数 然后ans[i]记录第一个串前i个字符所有涂成b钱i个字符的步数 那么当来了b 一个字符 如果相等那么ana[i]=min(dp[0][i],ans[i-1]) 然后还可能是前面随意一个涂好的ans[j] 再见过dp[j+1][i]而来 */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #include<set> #include<map> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) #define eps 1e-8 typedef __int64 ll; using namespace std; #define N 105 int dp[N][N],s[N]; char a[N],b[N]; int main() { int i,j; while(~scanf("%s",a)) { scanf("%s",b); memset(dp,0,sizeof(dp)); int len=strlen(b); for(i=0;i<len;i++) dp[i][i]=1; for(i=len-1;i>=0;i--) for(j=i+1;j<len;j++) { 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(i=0;i<len;i++) { s[i]=dp[0][i]; if(a[i]==b[i]) { if(i==0) s[i]=0; else s[i]=s[i-1]; } else for(int k=0;k<i;k++) s[i]=min(s[i],s[k]+dp[k+1][i]); } printf("%d\n",s[len-1]); } return 0; }
以上是关于HDU 2476 String painter(区间dp)的主要内容,如果未能解决你的问题,请参考以下文章