hdu 2476 String painter(区间dp)

String painter

                                                                                               Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                                                      Total Submission(s): 4108    Accepted Submission(s): 1915
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

明天来解释 我想洗澡睡觉了

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cctype>
 5 #include<cmath>
 6 #include<cstring>
 7 #include<map>
 8 #include<stack>
 9 #include<set>
10 #include<vector>
11 #include<algorithm>
12 #include<string.h>
13 typedef long long ll;
14 typedef unsigned long long LL;
15 using namespace std;
16 const int INF=0x3f3f3f3f;
17 const double eps=0.0000000001;
18 const int N=1000+10;
19 char a[N],b[N];
20 int dp[N][N];
21 int DP[N];
22 int main(){
23     while(gets(a)){
24         gets(b);
25         int len=strlen(a);
26         memset(dp,INF,sizeof(dp));
27         for(int i=0;i<len;i++)dp[i][i]=1;
28         for(int t=1;t<len;t++){
29             for(int i=0;i+t<len;i++){
30                 int j=i+t;
31                 if(b[i]==b[j])dp[i][j]=dp[i][j-1];
32                 else{
33                     for(int k=i;k<j;k++)
34                     dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
35                 }
36             }
37         }
38         for(int i=0;i<len;i++){
39             if(i==0&&a[i]==b[i])dp[0][i]=0;
40             else if(a[i]==b[i])dp[0][i]=dp[0][i-1];
41             for(int j=0;j<i;j++)
42             dp[0][i]=min(dp[0][i],dp[0][j]+dp[j+1][i]);
43         }
44         cout<<dp[0][len-1]<<endl;
45     }
46 }
时间: 2024-11-08 18:34:12

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

HDU 2476 String painter(字符串转变)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 题意:给定两个长度相同的串A和B.每次操作可以将A的连续一段改变为另一个字母.求将A转换成B最少需要多少次操作? 思路:首先,我们假设没有A串,那么这就跟 BZOJ1260是一样的了,即答案为DFS(0,n-1)...但是这里有了A串就有可能使得操作次数更少.因为可能有些对应位置字母是相同的.我们设 ans[i]表示前i个字母变成一样的,那么若A[i]=B[i]则ans[i]=ans[i-1]

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

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

HDU 2476 String painter (区间DP)

题意:给出两个串a和b,一次只能将一个区间刷一次,问最少几次能让a=b 思路:首先考虑最坏的情况,就是先将一个空白字符串刷成b需要的次数,直接区间DP[i][j]表示i到j的最小次数. 再考虑把a变成b的次数 ans[i]:a从(0,i)变成b(0,i)所需的最小次数 初始化ans[i]=dp[0][i] 如果a[i]==b[i],则ans[i]=ans[i-1]; 由小区间更新到大区间 1 //#pragma comment(linker, "/STACK:167772160")//

HDU 2476 String painter(记忆化搜索, DP)

题目大意: 给你两个串,有一个操作! 操作时可以把某个区间(L,R) 之间的所有字符变成同一个字符.现在给你两个串A,B要求最少的步骤把A串变成B串. 题目分析: 区间DP, 假如我们直接想把A变成B,那么我们DP区间的时候容易产生一个问题:假如我这个区间更新了,那么之前这个区间的子区间内DP出来的值就没用. 然后考虑到这里一直想不过去.最后看了看题解才知道. 我们可以先预处理一下怎么将一个空串变成B串需要的操作数. 这样我们就不用考虑子区间被覆盖的情况了. 如区间L,R 我们需要考虑的是点L是

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) 字符串刷子

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

hdu 2476 String Painter

第一道区间dp题,感觉题意不是很好理解 题意:一次可以转换某一个位置的字符,或是一串连续的字符,举第一个例子zzzzzfzzzzz 1:aaaaaaaaaaa 2: abbbbbbbbba 3: abcccccccba 4: abcdddddcba 5: abcdeeedcba 6: abcdefedcba 于是第一个例子输出6,第二个同理 话不多说,直接贴一波代码 1 #include<iostream> 2 #include<cstdio> 3 #include<cstr

HDU 2476 String painter 刷字符串(AC代码)区间DP

题意:给出两个串s1和s2,一次只能将一个区间刷一次,问最少几次能让s1=s2 例如zzzzzfzzzzz,长度为11,我们就将下标看做0~10 先将0~10刷一次,变成aaaaaaaaaaa 1~9刷一次,abbbbbbbbba 2~8:abcccccccba 3~7:abcdddddcba 4~6:abcdeeedcab 5:abcdefedcab 这样就6次,变成了s2串了 第二个样例也一样 先将0~10刷一次,变成ccccccccccb 1~9刷一次,cdddddddddcb 2~8:c