码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2476 String painter(区间dp)

时间:2017-05-10 01:05:36      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:main   title   segment   algorithm   end   tput   std   other   output   

                                             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
明天来解释 我想洗澡睡觉了
 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 }

 

 

hdu 2476 String painter(区间dp)

标签:main   title   segment   algorithm   end   tput   std   other   output   

原文地址:http://www.cnblogs.com/Aa1039510121/p/6833439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!