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
  1. apple peach
  2. ananas banana
  3. pear peach
 
Sample Output
  1. appleach
  2. bananas
  3. pearch
 
  1. /*
  2. 这道题是这样输出的。首先读取s1的字符,读取时推断是否是公共字符。假设是就把s1前面的字符所有输出,
  3. 然后就来推断s2。(和推断s1是一样的),然后把公共字符输出,一直这样推断,直到最后一个公共字符
  4. 当最后一个公共字符推断完了后,把剩下的输出,先输s1,再s2.
  5.  
  6. */
  7.  
  8. #include<stdio.h>
  9. #include<string.h>
  10. #define max(a,b) (a)>(b)?
  11.  
  12. (a):(b)
  13. char s1[200],s2[200];
  14. int dp[200][200];
  15. struct subsequence
  16. {
  17. int i,j;
  18. char ch;
  19. }common[120];
  20. int main()
  21. {
  22. int i,j,k;
  23. int len1,len2;
  24. while(~scanf("%s%s",s1,s2))
  25. {
  26. len1=strlen(s1);
  27. len2=strlen(s2);
  28. memset(dp,0,sizeof(dp));
  29. for(i=1;i<=len1;++i) //LCS
  30. {
  31. for(j=1;j<=len2;++j)
  32. {
  33. if(s1[i-1]==s2[j-1]) dp[i][j]=dp[i-1][j-1]+1;
  34. else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
  35. }
  36. }
  37. if(dp[len1][len2]==0)//假设没有公共的,直接输出
  38. {
  39. printf("%s%s\n",s1,s2);
  40. continue;
  41. }
  42. else//有就開始记录 。倒着记录
  43. {
  44. i=len1;j=len2;
  45. k=0;
  46. while(i>=1&&j>=1)
  47. {
  48. if(dp[i][j]==dp[i-1][j-1]+1&&s1[i-1]==s2[j-1])//最后一位同样,就存起来
  49. {
  50. common[k].i=i-1;//记录s1串的公共子序列(最后一个。倒数第二个.....) 元素的位置
  51. common[k].j=j-1;//记录s2串的公共子序列(最后一个,倒数第二个.....) 元素的位置
  52. common[k].ch=s1[i-1];//记录该公共字符
  53. i--,j--;
  54. k++;
  55. }
  56. else if(dp[i-1][j]>dp[i][j-1])//当去掉s1的最后一个元素的s1比去掉s2最后一个元素的最大公共子序列还要大的时候,说明s1的末尾不是最长子序列的一部分
  57. i--;
  58. else
  59. j--;
  60. }
  61. }
  62. i=j=0;
  63. for(k=k-1;k>=0;--k)
  64. {
  65. while(common[k].i!=i)//先输出s1
  66. {
  67. printf("%c",s1[i]);
  68. ++i;
  69. }
  70. while(common[k].j!=j)//再输s2
  71. {
  72. printf("%c",s2[j]);
  73. ++j;
  74. }
  75. printf("%c",common[k].ch);
  76. ++i,++j;
  77. }
  78. while(s1[i]!='\0')//输出剩下的
  79. {
  80. printf("%c",s1[i]);
  81. ++i;
  82. }
  83. while(s2[j]!='\0')
  84. {
  85. printf("%c",s2[j]);
  86. ++j;
  87. }
  88. puts("");
  89. }
  90.  
  91. return 0;
  92. }

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. linux下的开源移动图像监测程序--motion编译与配置【转】

    本文转载自:http://www.cnblogs.com/qinyg/p/3355707.html 前几天在网上偶然看到一篇博客,是利用linxu下的开源的motion搭建嵌入式视频动态监控系统,感觉 ...

  2. 2018亚洲区预选赛北京赛站网络赛 D.80 Days 尺取

    题面 题意:你带着K元要去n个城市,这n个城市是环形的,你可以选择任意一个起点,然后顺时针走,对于每个城市,到达时可以获得a元,但是从这里离开又需要花费b元,问你能否找到一个起点(输出花钱最少的那个) ...

  3. Elasticsearch之curl创建索引库和索引时注意事项

    前提, Elasticsearch之curl创建索引库 Elasticsearch之curl创建索引 注意事项 1.索引库名称必须要全部小写,不能以下划线开头,也不能包含逗号 2.如果没有明确指定索引 ...

  4. Super超级ERP系统---(10)订单打包

    订单拣货完成后,需要把订单装箱打包,并打印客户地址信息.订单打包的操作流程先是扫描订单号,然后扫描商品条码.  1.订单打包 打印包装箱面单 2.订单发货 订单打包完成后就等待发货,快递公司来拉货的时 ...

  5. P1732 活蹦乱跳的香穗子

    题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现自己很无聊,于是她想了一个有趣的事情,每个格子最多只能经过1次,且每个格子都有其价值 跳的规则是这样的,香穗子可以向上下左右四个方向跳到相邻的格子,并且她 ...

  6. Kafka 分布式消息系统详解

    实际上kafka对机器的需求与Hadoop的类似. 原来,对于Linkin这样的互联网企业来说,用户和网站上产生的数据有三种: 需要实时响应的交易数据,用户提交一个表单,输入一段内容,这种数据最后是存 ...

  7. VS2012 +PTVS配置

    使用PTVS可以在Vs中使用Python,可以作为一个小TIps,总体来说还是不太合算,有多少价值呢!? 可以使用Python,可以直接构建Python工程.... 我下载的是 2.0beta版本: ...

  8. 杭电1003 Max Sum TLE

    这一题目是要求连续子序列的最大和,所以在看到题目的一瞬间就想到的是把所有情况列举出来,再两个两个的比较,取最大的(即为更新最大值的意思),这样的思路很简单,但是会超时,时间复杂度为O(n^3),因为有 ...

  9. 读书笔记「Python编程:从入门到实践」_2.变量和简单数据类型

    做了大半年RPA了,用的工具是Kapow. 工作没有那么忙,不想就这么荒废着,想学点什么.就Python吧. 为期三个月,希望能坚持下来. 2.1 变量的命名和使用 变量名只能包含字母.数字和下划线. ...

  10. 脚本自动化部署varnish源码包

    #!bin/bash#功能:自动化部署 varnish 源码包软件,运行脚本前,需提前下载 varnish-5.0.0.tar.gz#作者:liusingbonyum -y install gcc r ...