Advanced Fruits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3340 Accepted Submission(s): 1714
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
这道题就是给你两个单词,然后你要把两个单词拼接成一个新单词,使得新单词的子序列中包含两个单词,并且要使这个新单词最短。所以这道题就是求最长公共子序列,并且要记录下子序列的字母,以及他们在主串和副串中的原始位置,之后进行拼接输出。
1 #include<stdio.h> 2 #include<string.h> 3 4 const int maxn=210; 5 6 char str1[maxn],str2[maxn]; 7 int dp[maxn][maxn],len; //len指示ans的长度 8 9 struct node{ 10 int i,j; //i记录主串位置,j记录副串当前字符位置 11 char ch; //记录当前字符 12 }ans[maxn]; 13 14 int max(int a,int b){ 15 return a>b?a:b; 16 } 17 18 void LCS(int m,int n){ 19 memset(dp,0,sizeof(dp)); 20 int i,j; 21 for(i=1;i<=m;i++) 22 for(j=1;j<=n;j++) 23 if(str1[i]==str2[j]) 24 dp[i][j]=dp[i-1][j-1]+1; 25 else 26 dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 27 if(dp[m][n]==0){ //如果没有公共序列,直接输出 28 printf("%s%s",str1,str2); 29 }else{ 30 i=m;j=n; 31 len=0; 32 while(i!=0 && j!=0){ //取出最长公共子序列的字母 33 if((dp[i][j]==dp[i-1][j-1]+1) && str1[i]==str2[j]){ 34 ans[len].i=i; 35 ans[len].j=j; 36 ans[len++].ch=str1[i]; //倒序保存最长公共子序列字母 37 i--;j--; 38 }else if(dp[i-1][j]>dp[i][j-1]) 39 i--; 40 else 41 j--; 42 } 43 } 44 } 45 46 int main(){ 47 48 //freopen("input.txt","r",stdin); 49 50 int len1,len2,i,j,k; 51 while(scanf("%s%s",str1+1,str2+1)!=EOF){ 52 len1=strlen(str1+1); 53 len2=strlen(str2+1); 54 LCS(len1,len2); 55 i=j=1; 56 for(k=len-1;k>=0;k--){ 57 while(i!=ans[k].i){ 58 printf("%c",str1[i]); 59 i++; 60 } 61 while(j!=ans[k].j){ 62 printf("%c",str2[j]); 63 j++; 64 } 65 printf("%c",ans[k].ch); 66 i++;j++; 67 } 68 printf("%s%s ",str1+1+ans[0].i,str2+1+ans[0].j); 69 } 70 return 0; 71 }
根据LCS的原理,将每个字符都进行标记,看两个字符串中对应的字符究竟处于什么状态,然后输出,其标记为公共子串的字符只输出一次即可
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 char s1[1000],s2[1000]; 7 int len1,len2,dp[1000][1000],mark[1000][1000]; 8 9 void LCS() 10 { 11 int i,j; 12 memset(dp,0,sizeof(dp)); 13 for(i = 0;i<=len1;i++) 14 mark[i][0] = 1; 15 for(i = 0;i<=len2;i++) 16 mark[0][i] = -1; 17 for(i = 1; i<=len1; i++) 18 { 19 for(j = 1; j<=len2; j++) 20 { 21 if(s1[i-1]==s2[j-1]) 22 { 23 dp[i][j] = dp[i-1][j-1]+1; 24 mark[i][j] = 0; 25 } 26 else if(dp[i-1][j]>=dp[i][j-1]) 27 { 28 dp[i][j] = dp[i-1][j]; 29 mark[i][j] = 1; 30 } 31 else 32 { 33 dp[i][j] = dp[i][j-1]; 34 mark[i][j] = -1; 35 } 36 } 37 } 38 } 39 40 void PrintLCS(int i,int j) 41 { 42 if(!i && !j) 43 return ; 44 if(mark[i][j]==0) 45 { 46 PrintLCS(i-1,j-1); 47 printf("%c",s1[i-1]); 48 } 49 else if(mark[i][j]==1)//根据回溯的位置进行输出 50 { 51 PrintLCS(i-1,j); 52 printf("%c",s1[i-1]); 53 } 54 else 55 { 56 PrintLCS(i,j-1); 57 printf("%c",s2[j-1]); 58 } 59 } 60 61 int main() 62 { 63 while(~scanf("%s%s",s1,s2)) 64 { 65 len1 = strlen(s1); 66 len2 = strlen(s2); 67 LCS(); 68 PrintLCS(len1,len2); 69 printf(" "); 70 } 71 72 return 0; 73 }