1. 程式人生 > >Advanced Fruits HDU杭電1503【LCS的保存】

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

style pro cte rac data- sequence return arch its

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.

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.

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


/*
這道題是這樣輸出的。首先讀取s1的字符,讀取時推斷是否是公共字符。假設是就把s1前面的字符所有輸出,
然後就來推斷s2。(和推斷s1是一樣的),然後把公共字符輸出,一直這樣推斷,直到最後一個公共字符
當最後一個公共字符推斷完了後,把剩下的輸出,先輸s1,再s2.

*/

#include<stdio.h>
#include<string.h>
#define max(a,b) (a)>(b)?

(a):(b) char s1[200],s2[200]; int dp[200][200]; struct subsequence { int i,j; char ch; }common[120]; int main() { int i,j,k; int len1,len2; while(~scanf("%s%s",s1,s2)) { len1=strlen(s1); len2=strlen(s2); memset(dp,0,sizeof(dp)); for(i=1;i<=len1;++i) //LCS { for(j=1;j<=len2;++j) { if(s1[i-1]==s2[j-1]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); } } if(dp[len1][len2]==0)//假設沒有公共的,直接輸出 { printf("%s%s\n",s1,s2); continue; } else//有就開始記錄 。倒著記錄 { i=len1;j=len2; k=0; while(i>=1&&j>=1) { if(dp[i][j]==dp[i-1][j-1]+1&&s1[i-1]==s2[j-1])//最後一位同樣,就存起來 { common[k].i=i-1;//記錄s1串的公共子序列(最後一個。倒數第二個.....) 元素的位置 common[k].j=j-1;//記錄s2串的公共子序列(最後一個,倒數第二個.....) 元素的位置 common[k].ch=s1[i-1];//記錄該公共字符 i--,j--; k++; } else if(dp[i-1][j]>dp[i][j-1])//當去掉s1的最後一個元素的s1比去掉s2最後一個元素的最大公共子序列還要大的時候,說明s1的末尾不是最長子序列的一部分 i--; else j--; } } i=j=0; for(k=k-1;k>=0;--k) { while(common[k].i!=i)//先輸出s1 { printf("%c",s1[i]); ++i; } while(common[k].j!=j)//再輸s2 { printf("%c",s2[j]); ++j; } printf("%c",common[k].ch); ++i,++j; } while(s1[i]!='\0')//輸出剩下的 { printf("%c",s1[i]); ++i; } while(s2[j]!='\0') { printf("%c",s2[j]); ++j; } puts(""); } return 0; }




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