H - Advanced Fruits

题目:

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

题意:

给你两个字符串,然后进行合并;

思路:

先找出两个字符串的公共子串,然后把剩下的字符加进去;

例子:apple peach

子串:pe

加入后:(a)p(pl)e(ach)

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

char a[101],b[101];
char s[201];
int dp[101][101];

int main()
{
    while(~scanf("%s%s",a+1,b+1))//把字符串的下标从1开始;
    {
        int i,j;
        int la=strlen(a+1);
        int lb=strlen(b+1);
        //printf("%d %d\n",la,lb);
        memset(dp,0,sizeof dp);
        for(i=1; i<=la; i++)//找出LCS;
        {
            for(j=1; j<=lb; 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]);
            }
        }
        /*for(i=0;i<=la;i++)
        {
            for(j=0;j<=lb;j++)
            {
                printf("%d ",dp[i][j]);
            }
            printf("\n");
        }*/
        i=la;
        j=lb;
        int k=0;
        while(dp[i][j])//先把LCS中的字符串加入数组中;
        {
            if(a[i]==b[j])//如果两个数组中的字符相同
            {
                s[k++]=a[i];
                i--;
                j--;
            }
            else if(dp[i][j-1]>=dp[i-1][j])//由上面的求LCS可以知道dp【i】【j】是由这两个中的一个得来的,
            {                               //所以可以知道上面的一个字符来自于哪个字符串;
                s[k++]=b[j];
                j--;
            }
            else if(dp[i][j-1]<dp[i-1][j])
            {
                s[k++]=a[i];
                i--;
            }
        }
        while(i>0)//把剩下的字符加入字符串中;
        {
            s[k++]=a[i--];
        }
        while(j>0)
        {
            s[k++]=b[j--];
        }
        for(i=k-1;i>=0;i--)//倒序输出;
        {
            printf("%c",s[i]);
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/titi2018815/article/details/81388530