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
 
/*
这道题是这样输出的。首先读取s1的字符,读取时推断是否是公共字符。假设是就把s1前面的字符所有输出,
然后就来推断s2。(和推断s1是一样的),然后把公共字符输出,一直这样推断,直到最后一个公共字符
当最后一个公共字符推断完了后,把剩下的输出,先输s1,再s2. */ #include<stdio.h>
#include<string.h>
#define max(a,b) (a)>(b)? (a):(b)
char s1[200],s2[200];
int dp[200][200];
struct subsequence
{
int i,j;
char ch;
}common[120];
int main()
{
int i,j,k;
int len1,len2;
while(~scanf("%s%s",s1,s2))
{
len1=strlen(s1);
len2=strlen(s2);
memset(dp,0,sizeof(dp));
for(i=1;i<=len1;++i) //LCS
{
for(j=1;j<=len2;++j)
{
if(s1[i-1]==s2[j-1]) dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
if(dp[len1][len2]==0)//假设没有公共的,直接输出
{
printf("%s%s\n",s1,s2);
continue;
}
else//有就開始记录 。倒着记录
{
i=len1;j=len2;
k=0;
while(i>=1&&j>=1)
{
if(dp[i][j]==dp[i-1][j-1]+1&&s1[i-1]==s2[j-1])//最后一位同样,就存起来
{
common[k].i=i-1;//记录s1串的公共子序列(最后一个。倒数第二个.....) 元素的位置
common[k].j=j-1;//记录s2串的公共子序列(最后一个,倒数第二个.....) 元素的位置
common[k].ch=s1[i-1];//记录该公共字符
i--,j--;
k++;
}
else if(dp[i-1][j]>dp[i][j-1])//当去掉s1的最后一个元素的s1比去掉s2最后一个元素的最大公共子序列还要大的时候,说明s1的末尾不是最长子序列的一部分
i--;
else
j--;
}
}
i=j=0;
for(k=k-1;k>=0;--k)
{
while(common[k].i!=i)//先输出s1
{
printf("%c",s1[i]);
++i;
}
while(common[k].j!=j)//再输s2
{
printf("%c",s2[j]);
++j;
}
printf("%c",common[k].ch);
++i,++j;
}
while(s1[i]!='\0')//输出剩下的
{
printf("%c",s1[i]);
++i;
}
while(s2[j]!='\0')
{
printf("%c",s2[j]);
++j;
}
puts("");
} return 0;
}

Advanced Fruits HDU杭电1503【LCS的保存】的更多相关文章

  1. 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

    今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...

  2. 一个人的旅行 HDU杭电2066【dijkstra算法 || SPFA】

    pid=2066">http://acm.hdu.edu.cn/showproblem.php? pid=2066 Problem Description 尽管草儿是个路痴(就是在杭电 ...

  3. 『ACM C++』HDU杭电OJ | 1418 - 抱歉 (拓扑学:多面体欧拉定理引申)

    呕,大一下学期的第一周结束啦,一周过的挺快也挺多出乎意料的事情的~ 随之而来各种各样的任务也来了,嘛毕竟是大学嘛,有点上进心的人多多少少都会接到不少任务的,忙也正常啦~端正心态 开心面对就好啦~ 今天 ...

  4. 杭电 1503 Advanced Fruits

    Description The company "21st Century Fruits" has specialized in creating new sorts of fru ...

  5. HDU Today HDU杭电2112【Dijkstra || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=2112 Problem Description 经过锦囊相助,海东集团最终度过了危机,从此.HDU的发展就一直顺风 ...

  6. Choose the best route HDU杭电2680【dijkstra算法 || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

  7. 畅通project续HDU杭电1874【dijkstra算法 || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了非常多年的畅通project计划后.最终修建了非常多 ...

  8. find the safest road HDU杭电1596【Dijkstra || SPFA】

    pid=1596">http://acm.hdu.edu.cn/showproblem.php?pid=1596 Problem Description XX星球有非常多城市,每一个城 ...

  9. 升级降级(期望DP)2019 Multi-University Training Contest 7 hdu杭电多校第7场(Kejin Player)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6656 题意: 有 1~n 个等级,你现在是1级,求升到n级的花费期望.会给你n个条件(i~i+1级升级 ...

随机推荐

  1. Tomcat启动时项目反复载入,导致资源初始化两次的问题

    近期在项目开发測试的时候,发现Tomcat启动时项目反复载入,导致资源初始化两次的问题  导致该问题的解决办法: 例如以下图:在Eclipse中将Server Locations设置为"Us ...

  2. luogu2766 最长不下降子序列问题 DP 网络流

    题目大意:给定正整数序列x1,...,xn .(1)计算其最长不下降子序列的长度s.(不一定是否连续)(2)计算从给定的序列中最多可取出多少个长度为s的不下降子序列.(序列内每一个元素不可重复)(3) ...

  3. h5-8 canvas

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. hdoj--2119--Matrix(最小点覆盖)

    Matrix Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  5. 谷歌浏览器(chrome) —— 扩展应用程序

    工具的使用--谷歌浏览器(chrome) (二) 1. 设置和下载方法 右上角菜单按钮 ⇒ 更多工具(more tools) ⇒ 扩展(Extensions) 打开该页面之后,会首先进入扩展(已安装应 ...

  6. 框架-Eureka:初识 Eureka

    ylbtech-框架-Eureka:初识 Eureka 1.返回顶部 1. 1.1. http://localhost:2100/ 1.2. 2. Eureka - Last N events 3. ...

  7. 一致性hash 算法 (转)

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179     一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT) ...

  8. JS 中构造函数和普通函数的区别(详)

    1.构造函数也是一个普通函数,创建方式和普通函数一样,但构造函数习惯上首字母大写 2.构造函数和普通函数的区别在于:调用方式不一样.作用也不一样(构造函数用来新建实例对象) 3.调用方式不一样. 普通 ...

  9. insert into 语句的三种写法 以及批量插入

    方式1. INSERT INTO t1(field1,field2) VALUE(v001,v002);            // 插入一条 方式2. INSERT INTO t1(field1,f ...

  10. Gitlab 灾备措施

    Gitlab创建备份 使用Gitlab一键安装包安装Gitlab非常简单,同样的备份恢复与迁移也非常简单.使用一条命令即可创建完整的Gitlab备份: gitlab-rake    gitlab:ba ...