HDOJ 题目2474 String painter(区间DP)

String painter

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2520    Accepted Submission(s):
1134

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 2474

题目大意:给你两串,每次操作能把a串的任意一段都变成任意同一个小写字母,问最少操作多少次可以变成b串

15MS 1452K
#include<stdio.h>
#include<string.h>
#define min(a,b) (a>b?b:a)
int dp[110][110],ans[110];
char str1[110],str2[110];
int main()
{
	while(scanf("%s%s",str1+1,str2+1)!=EOF)
	{
		int i,j,k;
		int len=strlen(str1+1);
		memset(dp,0,sizeof(dp));
		memset(ans,0,sizeof(ans));
		for(i=1;i<=len;i++)
			dp[i][i]=1;
		for(i=len-1;i>=1;i--)//a串和b串一个也不相等时
		{
			for(j=i+1;j<=len;j++)
			{
				dp[i][j]=dp[i+1][j]+1;
				for(k=i+1;k<=j;k++)
				{
					if(str2[i]==str2[k])
					{
						dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j]);
					}
				}
			}
		}
		for(i=1;i<=len;i++)
		{
			ans[i]=dp[1][i];
			if(str1[i]==str2[i])
			{
				ans[i]=ans[i-1];
			}
			else
			{
				for(j=1;j<i;j++)
				{
					ans[i]=min(ans[i],ans[j]+dp[j+1][i]);
				}
			}
		}
		printf("%d\n",ans[len]);
	}
}

  

时间: 2024-10-03 08:41:47

HDOJ 题目2474 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

uva live 4394 String painter 间隔dp

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

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

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

HDOJ题目2089 不要62(数位DP)

不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 25391    Accepted Submission(s): 8788 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就

POJ 题目3661 Running(区间DP)

Running Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5652   Accepted: 2128 Description The cows are trying to become better athletes, so Bessie is running on a track for exactly N (1 ≤ N ≤ 10,000) minutes. During each minute, she can