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 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的保存】)