Coin163

首页 > hdu 1503 Advanced Fruits DP LCS

hdu 1503 Advanced Fruits DP LCS

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

Advanced Fruits

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

hdu : http://acm.hdu.edu.cn/showproblem.php?pid=1503

 

题意:

两种水果杂交出一种新水果,现在给新水果取名,要求这个名字中包含了以前两种水果名字的字母,并且这个名字要尽量短。也就是说以前的一种水果名字arr1是新水果名字arr的子序列,另一种水果名字arr2也是新水果名字arr的子序列。要你求arr。

 

做法:

用求最长公共子序列的方法,求出arr1和arr2的最长公共子序列,然后再用递归思想,逐一输出,得到的就是最后答案。

#include <stdio.h>   

#include <string.h>   

#define size 201   

  

int Max(int x,int y)  

{  

    return x>y?x:y;  

}  

  

struct rem   

{  

    int i,j;/*i记录主串位置,j记录副串当前字符位置*/  

    char ch;/*记录当前字符*/  

};  

  

char a[size],b[size];  

int dp[size][size];  

rem ans[size];  

int len;/*指示ans的长度*/  

  

void lcs(int m,int n)  

{  

    int i,j;  

    memset(dp,0,sizeof(dp));  

    len = 0;  

    for (i=1;i<=m;i++)  

    {  

        for (j=1;j<=n;j++)  

        {  

            if(a[i]==b[j])  

                dp[i][j] = dp[i-1][j-1]+1;    

            else  

                dp[i][j] = Max(dp[i-1][j],dp[i][j-1]);  

        }  

    } 

        i = m,j = n;  

        while (i!=0&&j!=0)  

        {  

            if ((dp[i][j] = dp[i-1][j-1]+1)&&a[i] == b[j])  

            {  

                ans[len].i = i;  

                ans[len].j = j;  

                ans[len++].ch = a[i];/*倒序保存最长公共子序列字母*/  

                i--,j--;  

            }  

            else if(dp[i-1][j]>dp[i][j-1])  

                i--;  

            else  

                j--;  

        }  

        /*取出最长公共子序列的字母*/  

     

}  

  

int main()  

{  

    int a1,b1,i,j,k;  

    while (scanf("%s%s",a+1,b+1)!=EOF)  

    {  

        a1 = strlen(a+1);  

        b1 = strlen(b+1);  

        lcs(a1,b1);   

        i = j = 1;  

        for (k=len-1;k>=0;k--)  

        {  

            while (i!=ans[k].i)  

            {  

                printf("%c",a[i]);  

                i++;  

            }  

            while (j!=ans[k].j)  

            {  

                printf("%c",b[j]);  

                j++;  

            }  

            printf("%c",ans[k].ch);  

            i++,j++;  

        }  

        printf("%s%s\n",a+ans[0].i+1,b+ans[0].j+1);  

    }  

    return 0;  

}

 

 


 

原文

Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 877    Accepted Submission(s): 407 Special Judge Problem De

------分隔线----------------------------