首页 > ACM题库 > HDU-杭电 > HDU 1503 Advanced Fruits[解题报告] C++
2013
12-11

HDU 1503 Advanced Fruits[解题报告] C++

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.

输入:

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.

输出:

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.

样例输入:

apple peach
ananas banana
pear peach

样例输出:

appleach
bananas
pearch

题意:将两个单词合并,重复的部分只输出一次。

分析:最长递增子序列的变形,只是输出的地方发生了变化

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MN=200;
int len1,len2;
int b[MN][MN];
char s1[MN],s2[MN];
int c[MN][MN];
  
void LCSLenth()
{
    int i,j;
    memset(c,0,sizeof(c));
    for(i=1; i<=len1; i++)
    {
        for(j=1; j<=len2; j++)
        {
            if(s1[i-1]==s2[j-1])
            {
                c[i][j]=c[i-1][j-1]+1;
                b[i][j]=0;
            }
            else if(c[i][j-1]<=c[i-1][j])
            {
                c[i][j]=c[i-1][j];
                b[i][j]=1;//从上边
            }
            else
            {
                c[i][j]=c[i][j-1];
                b[i][j]=2;//从左边
            }
        }
    }
}
  
void Print(int i,int j)
{
    if(i==0 && j==0) return ;
    else if(i==0 && j!=0)
    {
        Print(i,j-1);
        printf("%c",s2[j-1]);
    }
    else if(i!=0 && j==0)
    {
        Print(i-1,j);
        printf("%c",s1[i-1]);
    }
    else if(b[i][j]==0)
    {
        Print(i-1,j-1);
        printf("%c",s1[i-1]);
    }
    else if(b[i][j]==1)
    {
        Print(i-1,j);
        printf("%c",s1[i-1]);
    }
    else
    {
        Print(i,j-1);
        printf("%c",s2[j-1]);//若从右边过来的,则打印s2
        //因为对于i是没发生变化的,也就是说s1序列位置没发生变化
    }
}
  
  
int main()
{
    int i,j;
    while(scanf("%s%s",&s1,&s2)!=EOF)
    {
        len1=strlen(s1);
        len2=strlen(s2);
        LCSLenth();
        Print(len1,len2);
        printf("\n");
    }
    return 0;
}

 

解题报告转自:http://www.cnblogs.com/zsboy/archive/2013/03/16/2963194.html


  1. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。