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

Advanced Fruits HDU - 1503

时间:2017-05-07 01:07:38      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:origin   arc   instance   namespace   like   oid   top   compute   ==   

  The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn‘t work, but sometimes, in very rare cases, a new fruit emerges that tastes like a mixture between both of them.
A big topic of discussion inside the company is "How should the new creations be called?" A mixture between an apple and a pear could be called an apple-pear, of course, but this doesn‘t sound very interesting. The boss finally decides to use the shortest string that contains both names of the original fruits as sub-strings as the new name. For instance, "applear" contains "apple" and "pear" (APPLEar and apPlEAR), and there is no shorter string that has the same property.

A combination of a cranberry and a boysenberry would therefore be called a "boysecranberry" or a "craboysenberry", for example.

Your job is to write a program that computes such a shortest name for a combination of two given fruits. Your algorithm should be efficient, otherwise it is unlikely that it will execute in the alloted time for long fruit names.
InputEach line of the input contains two strings that represent the names of the fruits that should be combined. All names have a maximum length of 100 and only consist of alphabetic characters.

Input is terminated by end of file.
OutputFor each test case, output the shortest name of the resulting fruit on one line. If more than one shortest name is possible, any one is acceptable.
Sample Input

apple peach
ananas banana
pear peach

Sample Output

appleach
bananas
pearch

1.~~~~为什么会这么写呢?
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<string>
 6 using namespace std;
 7 
 8 int dp[105][105];
 9 string s1,s2;
10 
11 void DFS(int i,int j){
12     if(i+j==0) return;
13     else if(i==0){
14         for(int k=0;k<j;k++) printf("%c",s2[k]);
15         return;
16     }
17     else if(j==0){
18         for(int k=0;k<i;k++) printf("%c",s1[k]);
19         return;
20     }
21     if(s1[i-1]==s2[j-1]){
22         DFS(i-1,j-1);
23         printf("%c",s1[i-1]);
24     }
25     else{
26         if(dp[i-1][j]>dp[i][j-1]){
27             DFS(i-1,j);
28             printf("%c",s1[i-1]); 
29         }
30         else{
31             DFS(i,j-1);
32             printf("%c",s2[j-1]);
33         }
34     } 
35 }
36 int main()
37 {   while(cin>>s1>>s2){
38         int len=s1.size(),lenth=s2.size();
39         memset(dp,0,sizeof(dp));
40         for(int i=1;i<=len;i++){
41             for(int j=1;j<=lenth;j++){
42                 if(s1[i-1]==s2[j-1]) dp[i][j]=dp[i-1][j-1]+1;
43                 else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
44             }
45         }
46         DFS(len,lenth);
47         cout<<endl;
48     }
49 }

 

Advanced Fruits HDU - 1503

标签:origin   arc   instance   namespace   like   oid   top   compute   ==   

原文地址:http://www.cnblogs.com/zgglj-com/p/6819208.html

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