Advanced Fruits HDU - 1503

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 }
时间: 2024-10-29 19:10:54

Advanced Fruits HDU - 1503的相关文章

Advanced Fruits HDU杭电1503【LCS的保存】

Problem Description 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

HDU 1503 Advanced Fruits (LCS,DP)

题意:给你两字符串s1,s2,用最短的字符串表示他们(公共字串输出一次). Sample Input apple peach ananas banana pear peach Sample Output appleach bananas pearch dp[i][j] : 第一个字符串的前 i 个 ,和第二个字符串的前 j 个最短组合的长度 . pre[i][j] : 第一个字符串的第 i 个 ,和第二个字符串的第 j 个字符的状态. #include<cstdio> #include<

HDU 1503 Advanced Fruits

Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3622    Accepted Submission(s): 1872Special Judge Problem Description The company "21st Century Fruits" has specialized in cr

Advanced Fruits(HDU 1503 LCS变形)

Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2358    Accepted Submission(s): 1201Special Judge Problem Description The company "21st Century Fruits" has specialized in cr

HDU 1503 Advanced Fruits[ LCS ]

题目:HDU 1503 思路:先求出最长公共子序列,记录路径.后进行拼接. 代码 #include<cstdio> #include<cstring> #include<cstring> #include<vector> #include<iostream> #include<algorithm> #define mod 1000000007 using namespace std; typedef long long LL; int

hdu 1503 LCS输出路径【dp】

hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左走.我用1表示向上走,2向左上方走,3向左走. 刚开始输入字符串时有两种方法,直接输入:或从地址后一位输入,即此时数组起始编号为1.直接输入时,dp[i][j]表示的是以s1[i-1],s2[j-1]为结尾LCS,另一种则就是表示以s1[i],s2[j]为结尾的LCS.两者在路径输出时有些差别,以前

HDU1503:Advanced Fruits 【LCS】

Advanced Fruits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 2 Special Judge Problem Description The company "21st Century Fruits" has specialized in creating

poj 2264 Advanced Fruits(DP)

Advanced Fruits Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1944   Accepted: 967   Special Judge Description The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit

HDU 1503 Advanced Fruits(LCS+记录路径)

http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是要我们求LCS,记录好路径就好. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<sstream> 6 #inc