HDU 1503 Advanced Fruits (公共子序列+輸出)

Mr_Treeeee發表於2020-04-06

Advanced Fruits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3619    Accepted Submission(s): 1869
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. 
 

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
 

Source
 

找到最長公共子序列,在輸出兩個字串,要合併子序列。
#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
int dp[1200][1200];
int f1[1200],f2[1200];
int main()
{
    char s1[1200];
    char s2[1200];
    while(~scanf("%s %s",s1,s2))
    {
        memset(dp,0,sizeof dp);
        memset(f1,-1,sizeof f1);
        memset(f2,-1,sizeof f2);
        int t1=strlen(s1);
        int t2=strlen(s2);
        for(int i=0; i<t1; i++)
        {
            for(int j=0; j<t2; j++)
            {
                if(s1[i]==s2[j])
                    dp[i+1][j+1]=dp[i][j]+1;
                else
                    dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
            }
        }
        int i=t1,j=t2,k=0;
        int pathi[1200],pathj[1200];
        while(dp[i][j])
        {
            if(dp[i][j]==dp[i-1][j])
                i--;
            else if(dp[i][j]==dp[i][j-1])
                j--;
            else
            {
                pathj[k]=j-1;
                pathi[k++]=i-1;
                i--;
                j--;
            }
        }
        int p=0;
        for(int i=k-1; i>=0; i--)
        {
            f1[pathi[i]]=++p;
            f2[pathj[i]]=p;
        }
        /*   for(int i=0;i<t1;i++)
           {
               printf("%d ",f1[i]);
           }
           printf("\n");
           for(int i=0;i<t2;i++)
           {
               printf("%d ",f2[i]);
           }
           printf("\n");*/
        int l1=0,l2=0;
        p=1;
        while(l1<t1&&l2<t2)
        {
            if(f1[l1]==-1)
            {
                printf("%c",s1[l1]);
                l1++;
            }
            else if(f2[l2]==-1)
            {
                printf("%c",s2[l2]);
                l2++;
            }
            else if(f1[l1]==p)
            {
                printf("%c",s1[l1]);
                p++;
                l1++;
                l2++;
            }
        }
        while(l1<t1)
        {
            printf("%c",s1[l1]);
            l1++;
        }
        while(l2<t2)
        {
            printf("%c",s2[l2]);
            l2++;
        }
        printf("\n");
    }
    return 0;
}


相關文章