现在的位置: 首页 > 综合 > 正文

HDU 1503Advanced Fruits LCS变形之字母存储+递归

2014年04月05日 ⁄ 综合 ⁄ 共 2634字 ⁄ 字号 评论关闭

点击打开链接

 

Advanced Fruits

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

 

Recommend
linle

题意:

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

做法:

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

#include<stdio.h>
#include<string.h>
char s1[107],s2[107];
int dp[1007][1007];
int len1,len2,i,j,k,len=0;//len指lcs的长度
struct node
{
    int pi;//记录主串位置
    int pj;//记录副串位置
    char c;//记录字符
}ans[1007];
int max(int a,int b)
{
    return a>b?a:b;
}
void lcs(int m,int n)
{
    len=0;
    memset(dp,0,sizeof(dp));
    for(i=1;i<=m;i++)
    for(j=1;j<=n;j++)
    {
        if(s1[i]==s2[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)&&s1[i]==s2[j])
        {
            ans[len].pi=i;
            ans[len].pj=j;
            ans[len++].c=s1[i];//倒序记录下来
            i--,j--;
        }
        else if(dp[i-1][j]>dp[i][j-1])
        i--;
        else
        j--;
    }
}
int main()
{
    while(scanf("%s%s",s1+1,s2+1)!=EOF)
    {
        len1=strlen(s1+1);
        len2=strlen(s2+1);
        lcs(len1,len2);
        i=j=1;
        for(k=len-1;k>=0;k--)
        {
            while(i!=ans[k].pi)
            {
                printf("%c",s1[i]);
                i++;
            }
            while(j!=ans[k].pj)
            {
                printf("%c",s2[j]);
                j++;
            }
            printf("%c",ans[k].c);
            i++,j++;
        }
        printf("%s%s\n",s1+ans[0].pi+1,s2+ans[0].pj+1);//最长子序列结束之后剩余的s1和s2的单词
    }
    return 0;
}

 

抱歉!评论已关闭.