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
 
//这是看的别人的代码,嗯,原来自己对于LCS的理解还不到位。
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
char str1[],str2[];
int dp[][],mark[][];
int len1,len2; void LCS()
{
memset(dp,,sizeof(dp));
for(int i=;i<=len1;i++)
mark[i][]=;
for(int i=;i<=len2;i++)
mark[][i]=-;
for(int i=;i<=len1;i++)
for(int j=;j<=len2;j++)
{
if(str1[i-]==str2[j-])
{
dp[i][j]=dp[i-][j-]+;
mark[i][j]=;
}
else if(dp[i-][j]>dp[i][j-])
{
dp[i][j]=dp[i-][j];
mark[i][j]=;
}
else
{
dp[i][j]=dp[i][j-];
mark[i][j]=-;
}
}
} void printfLCS(int i,int j)
{
if(i==&&j==)
return ;
if(mark[i][j]==)
{
printfLCS(i-,j-);
cout<<str1[i-];
}
else if(mark[i][j]==)
{
printfLCS(i-,j);
cout<<str1[i-];
}
else
{
printfLCS(i,j-);
cout<<str2[j-];
}
}
int main()
{
while(cin>>str1>>str2)
{
len1=strlen(str1);
len2=strlen(str2);
LCS();
printfLCS(len1,len2);
cout<<endl;
}
return ;
}

HDU1503:Advanced Fruits(LCS)的更多相关文章

  1. hdu 1503 Advanced Fruits(LCS输出路径)

    Problem Description The company "21st Century Fruits" has specialized in creating new sort ...

  2. HDU 1503 Advanced Fruits (LCS,变形)

    题意: 给两个水果名,要求他们的LCS部分只输出1次,其他照常输出,但是必须保持原来的顺序! 思路: 求LCS是常规的,但是输出麻烦了,要先求LCS,再标记两串中的所有LCS字符,在遇到LCS字符时, ...

  3. Advanced Fruits(HDU 1503 LCS变形)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. Advanced Fruits(好题,LCS的模拟)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. HDU-1053:Advanced Fruits(LCS+路径保存)

    链接:HDU-1053:Advanced Fruits 题意:将两个字符串合成一个串,不改变原串的相对顺序,可将相同字母合成一个,求合成后最短的字符串. 题解:LCS有三种状态转移方式,将每个点的状态 ...

  6. hdu 1503:Advanced Fruits(动态规划 DP & 最长公共子序列(LCS)问题升级版)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. hdu 1503 Advanced Fruits(最长公共子序列)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  8. 最长公共子序列(加强版) Hdu 1503 Advanced Fruits

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. poj 2264 Advanced Fruits(DP)

    Advanced Fruits Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1944   Accepted: 967   ...

随机推荐

  1. java操作mongodb——连接数据库

    import com.mongodb.MongoClient; MongoClient mongoClient = new MongoClient(); 连接MongoDB实例,默认为localhos ...

  2. css animation 动画的制作

    上效果 效果描述:原来这些图片都绝对定位在最右边,并有一个css3 3D的旋转初始效果.随着动画的开始,依次向左移动,并旋转到0度.(主要用于引导页步骤的描述) 上代码: html布局 <div ...

  3. 制作自己的web字体

    今天教给大家制作自己的web字体

  4. hdu1035

    #include<stdio.h>#include<string.h>int step,n,m;int a[1010][1010];char map[11][11];void ...

  5. NEUQ1055谭浩强C语言(第三版)习题6.11

    //迭代公式不是很理解,写出来算了.. #include <stdio.h> #include <math.h> int main() { double x0,x1; int ...

  6. Python基础知识学习_Day1

    1,python介绍 诞生于1989年圣诞节,目前越来越受到业界认可.应用领域十分广泛 云计算: 云计算最火的语言, 典型应用OpenStack WEB开发: 众多优秀的WEB框架,众多大型网站均为P ...

  7. abap 一些小知识点的总结

    创建包含结构或表的内表: DATA: BEGIN OF it_tab.     INCLUDE TYPE/STRUCTURE name.       name:结构名或者表名 DATA: num TY ...

  8. Python基础(六)-内置函数

      map().filter().reduce() map(func,iterator) --> filter(func,iterator) --> reduce(func,iterato ...

  9. php添加扩展插件

    给PHP安装扩展的方式有好多 一.重新编译 进入PHP源码目录./configure --prefix=/usr/local/php ...[其他编译参数] 二.通过phpize添加扩展 进入PHP源 ...

  10. Learning from the CakePHP source code - Part II

    原文:http://debuggable.com/posts/learning-from-the-cakephp-source-code-part-ii:480f4dd6-57fc-4715-8709 ...