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;
}

最新文章

  1. CheckLogin
  2. HTML day0
  3. 【原】文本图片自适应高度小bug以及解决办法
  4. WORD自定义宏
  5. Unicode编码
  6. A trip through the Graphics Pipeline 2011_01
  7. [.ashx檔?泛型处理程序?]基础入门#5....ADO.NET 与 将DB里面的二进制图片还原 (范例下载 &amp; 大型控件的ImageField)
  8. IOS UINavigationController 操作相关集合
  9. C#中如何生成矢量图
  10. Linq转成sql后的分页方法
  11. ubuntu 配置Java jdk
  12. ERROR 1062 (23000): Duplicate entry &#39;1-1&#39; for key &#39;PRIMARY&#39;
  13. Sencha Touch 2 结合HTML5的本地存储创建数据库实现增、删、改、查
  14. 使用JDBC进行数据库的事务操作(2)
  15. Centos/linux下的JDK安装
  16. TCP的核心系列 — SACK和DSACK的实现(六)
  17. python3 Queue(单向队列)
  18. 含有Date属性的对象转化为Json
  19. UVA 10791 -唯一分解定理的应用
  20. oracle 11g密码过期问题解决方法

热门文章

  1. BZOJ 4517: [Sdoi2016]排列计数 错排+逆元
  2. Linux内核OOM机制的理解【转】
  3. ActionFilterAttribute
  4. Swift-理解值类型
  5. gdb的使用(转)
  6. rehat7.X下postgresql 11编译安装
  7. C - Puzzles
  8. 参数转对象 类似 ?camera=1&amp;travel=0&amp;faceScore=1
  9. 图像检索中为什么仍用BOW和LSH
  10. css—各浏览器下的背景色渐变