Advanced Fruits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1426 Accepted Submission(s):
719
Special Judge
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 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.
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.
Input
Each 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.
Input is terminated by end of file.
Output
For 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
这道题也是关于最长公共子序列的问题,但是,需要将公共子序列标记一下。刚开始做的时候想到是要求最长公共子序列,但是不知道怎么有效地将结果输出来,后来想想,只能将两个数组从后往前扫描一遍,将公共子序列单独处理,保存到另一个数组c中,最后将数组c倒序输出。
1 /* 2 例如 apple peach 3 p e a c h 4 0 0 0 0 0 0 5 a 0 0 0 1 1 1 6 p 0 1 1 1 1 1 7 p 0 1 1 1 1 1 8 l 0 1 1 1 1 1 9 e 0 1 2 2 2 2 10 */
11 #include<iostream> 12 #include <cstring> 13 using namespace std; 14 #define MAX 105 15 char ch1[MAX],ch2[MAX],ch[MAX]; 16 int dp[MAX][MAX]; 17 int max(int a,int b) 18 { 19 return a>b?a:b; 20 } 21 int main() 22 { 23 int i,j,k; 24 int m,n; 25 while(cin.getline(ch1,MAX,' ') && cin.getline(ch2,MAX,' ')) 26 { 27 m = strlen(ch1); 28 n = strlen(ch2); 29 for(i=0;i<=n;i++) 30 dp[0][i] = 0; 31 for(j=0;j<=m;j++) 32 dp[j][0] = 0; 33 for(i=1;i<=m;i++) 34 for(j=1;j<=n;j++) 35 if(ch1[i-1] == ch2[j-1]) 36 dp[i][j] = dp[i-1][j-1] + 1; 37 else 38 dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 39 //以上是求最长公共子序列 40 i = strlen(ch1),j = strlen(ch2); 41 k=0; 42 while(i!=0 || j!=0) //将所求的序列保存在数组ch中,从后往前依次比较两个数组 43 { 44 if(i==0) //说明数组ch2还有剩余元素 45 { 46 ch[k++] = ch2[j-1]; 47 j--; 48 continue; 49 } 50 else if(j==0) //说明数组ch1还有剩余元素 51 { 52 ch[k++] = ch1[i-1]; 53 i--; 54 continue; 55 } 56 else if(ch1[i-1] != ch2[j-1]) 57 { 58 if(dp[i][j] == dp[i][j-1]) 59 { 60 ch[k++] = ch2[j-1]; 61 j--; 62 continue; 63 } 64 else if(dp[i][j] == dp[i-1][j]) 65 { 66 ch[k++] = ch1[i-1]; 67 i--; 68 continue; 69 } 70 } 71 else 72 { 73 ch[k++] = ch1[i-1]; 74 i--;j--; 75 continue; 76 } 77 } 78 for (i=k-1;i>=0;i--) 79 cout<<ch[i]; 80 cout<<endl; 81 memset(ch1,0,sizeof(ch1)); 82 memset(ch2,0,sizeof(ch2)); 83 } 84 return 0; 85 }