关闭

HDU 2476 String painter(区间dp)

标签: dp
561人阅读 评论(0) 收藏 举报
分类:

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.
 

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;
}





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:193134次
    • 积分:5821
    • 等级:
    • 排名:第4701名
    • 原创:389篇
    • 转载:9篇
    • 译文:0篇
    • 评论:14条
    最新评论