hdu2476 String painter(区间dp)

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

参考: http://blog.csdn.net/libin56842/article/details/9708807

#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],ans[N];
char a[N],b[N];

int main()
{
   int i,j;
   while(~scanf("%s",a))
   {
   	  scanf("%s",b);
   	  int len=strlen(b);

   	  memset(dp,0,sizeof(dp));

   	  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++)
	  {
	  	  ans[i]=dp[0][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]);
   }
   return 0;
}
时间: 2024-10-15 12:37:55

hdu2476 String painter(区间dp)的相关文章

uva live 4394 String painter 区间dp

// uva live 4394 String painter // // 这一题是训练指南上dp专题的习题,初看之下认为仅仅是稍微复杂了一点 // 就敲阿敲阿敲,两个半小时后,发现例子过了.然而自己给出的数据跪了 // 交了也wa了,才发现,自己的方法是有问题的,假设是将两个串同一时候考虑 // 的话,比方: dp[i][j] 表示从i到j,s串刷成目标b串所须要的最小的花费 // 然后依据区间的端点的字符特点,进行状态转移,然而可能是我太搓了, // 发现这种状态转移是不正确的.比方edc和

hdu 2476 String painter(区间dp)

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

NYOJ 1067 Compress String(区间dp)

Compress String 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描述 One day,a beautiful girl ask LYH to help her complete a complicated task-using a new compression method similar to Run Length Encoding(RLE) compress a string.Though the task is difficult, LYH is

hdu2476——String painter

String painter Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1834    Accepted Submission(s): 814 Problem Description There are two strings A and B with equal length. Both strings are made up

uva live 4394 String painter 间隔dp

// uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他们跪在数据 // 还要wa.才发现,自己的方法是有问题的,假设是将两个串同一时候考虑 // 的话.比方: dp[i][j] 表示从i到j,s串刷成目标b串所须要的最小的花费 // 然后依据区间的端点的字符特点,进行状态转移.然而可能是我太搓了. // 发现这种状态转移是不正确的,比方edc和cde.

hdu2476String painter (区间DP)

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

hdu_2476_String painter(区间DP)

题目链接:hdu_2476_String painter 题意: 有a,b两字符串,现在你有一个刷子,每次可以任选一个区间,使这个区间变成你想要的字符,现在让你将a变成b,问最少刷多少次 题解: 考虑区间dp[i][j],表示从第i到第j最少需要刷的次数.这里要先算从空串到b的dp,然后根据这个来推a串到b串的最小次数. 因为如果a的整个区间需要重新构造,这个区间就相当于空串.状态转移方程具体看代码 1 #include<cstdio> 2 #include<cstring> 3

UVALive - 3363 String Compression 区间DP

题目大意:有一串字符串,现在有一种转换规则,如果字符串中出现循环的子串,可以将其转化为 :子串数量(子串) 现在问这个字符串的最短长度 解题思路:区间dp,然后分类讨论,这题的难点是如何再进行缩减 将情况分为两种 一种是区间刚好符合缩减情况的,找出该区间的循环节,看能否继续缩减即可 另一种情况就是普通的区间DP了 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #de

HDU 2476 String painter(区间DP啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 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 cha