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.

InputEach 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.

OutputFor 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 题目意思:这道题我一开始一直没有看懂样例,不知道这道题是怎么来组合最大公共子序列和其他的字符的,但是同学的一句话让我豁然开朗,所给的A串和B串都可以看成要求的新串的子序列,或者可以说
要求一个最小公共母序列。 解题思路:根据LCS的原理,我们需要考虑最长公共子序列的每一个字符的来源和求解过程,回溯整个求解过程,对照DP表,我们就能窥见端倪,只要打印最长公共子序列的来源路径即可。 表中灰色格格所代表的字符组成的字符串,或许就可以称之为最短公共母序列了 递归方法打印路径,
 #include<cstdio>
#include<cstring>
char a[],b[];
int dp[][],lena,lenb,mark[][];
void LCS()
{
int i,j;
memset(dp,,sizeof(dp));
for(i=;i<lena;++i)
mark[i][]=;///x轴
for(i=;i<lenb;++i)
mark[][i]=-;///y轴
for(i=;i<=lena;++i)
{
for(j=;j<=lenb;++j)
{
if(a[i-]==b[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 out(int x,int y)
{
if(!x&&!y)
return ;
else if(mark[x][y]==)
{
out(x-,y-);
printf("%c",a[x-]);
}
else if(mark[x][y]==)
{
out(x-,y);
printf("%c",a[x-]);
}
else if(mark[x][y]==-)
{
out(x,y-);
printf("%c",b[y-]);
}
} int main()
{
while(scanf("%s%s",&a,&b)!=EOF)
{
lena=strlen(a);
lenb=strlen(b);
LCS();
out(lena,lenb);
printf("\n");///注意换行
}
return ;
}

非递归方法,回溯,这个代码主要是我对照着DP表的模拟得到的
 #include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
using namespace std;
#define N 210
int dp[N][N];
char c;
int main()
{
char a[N];
char b[N];
int la,lb;
int i,j;
while(scanf("%s%s",a,b)!=EOF)
{
memset(dp,,sizeof(dp));
la=strlen(a);
lb=strlen(b);
for(i=; i<=la; i++)
{
for(j=; j<=lb; j++)
{
if(a[i-]==b[j-])
{
dp[i][j]=dp[i-][j-]+;
}
else
{
dp[i][j]=max(dp[i-][j],dp[i][j-]);
}
}
}
i=la;
j=lb;
stack<char>s;
while(dp[i][j])
{
if(i==||j==)
{
break;
}
if(dp[i][j]==dp[i-][j])
{
i--;
s.push(a[i]);
}
else if(dp[i][j]==dp[i][j-])
{
j--;
s.push(b[j]);
}
else if(dp[i][j]>dp[i-][j-])
{
i--;
j--;
s.push(a[i]);
}
} while(i!=)///剩下的字符
{
i--;
s.push(a[i]);
}
while(j!=)
{
j--;
s.push(b[j]);
}
while(!s.empty())
{
c=s.top();
printf("%c",c);
s.pop();
}
printf("\n");
}
return ;
}

 
 

Advanced Fruits (最大公共子序列的路径打印)的更多相关文章

  1. HDU 1503 Advanced Fruits(LCS+记录路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是 ...

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

    链接:HDU-1053:Advanced Fruits 题意:将两个字符串合成一个串,不改变原串的相对顺序,可将相同字母合成一个,求合成后最短的字符串. 题解: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. 最长公共子序列(加强版) Hdu 1503 Advanced Fruits

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

  5. ZOJ 1076 Gene Assembly(LIS+路径打印 贪心)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=76 题目大意:在一个DNA上,给定许多基因的起始位置和结束位置,求出这 ...

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

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

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

    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. Advanced Fruits HDU杭电1503【LCS的保存】

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

随机推荐

  1. 【模板】缩点(tarjan,DAG上DP)

    题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只 ...

  2. ABAP术语-SAP GUI for HTML

    SAP GUI for HTML 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/14/1104996.html An ITS impleme ...

  3. 利用mysqlbinlog_flashback闪回丢失数据

            today,i'll have a test with the open source tool mysqlbinlog_flashback which is released by ...

  4. 【读书笔记 - Effective Java】04. 通过私有构造器强化不可实例化的能力

    工具类(utility class)不希望被实例化,比如只包含静态方法和静态域的类.为了这个目的,需要让这个类包含一个私有构造器. // 私有构造器示例 public class UtilityCla ...

  5. vue实现多级弹窗

    webpack + vue 实现 弹窗功能 对于刚入门webpack + vue 不久的新人来说,这技术,确实有些不太友好,相比较于直接操纵dom元素的jQuery,直接操纵数据的 vue 在webp ...

  6. 用Head方法获得百度搜索结果的真实地址

    用Head方法获得百度搜索结果的真实地址 在百度中搜索"Java",第一条结果的链接为: https://www.baidu.com/link?url=HBOOMbhPKH4SfI ...

  7. Python学习:7.文件操作

    文件操作 我们曾将听过一个问题,将大象放入冰箱分为三步:1.打开冰箱门,2.将大象放进去,3.关上冰箱门.今天我们要讲的Python文件操作的步骤就像将大象放入冰箱的步骤一样. 使用Python操作文 ...

  8. Java学习笔记十七:Java中static使用方法

    Java中static使用方法 一:Java中的static使用之静态变量: 我们都知道,我们可以基于一个类创建多个该类的对象,每个对象都拥有自己的成员,互相独立.然而在某些时候,我们更希望该类所有的 ...

  9. DevExpress 操作gridcontrol

    XtraGrid的关键类就是:GridControl和GridView.GridControl本身不显示数据,数据都是显示在GridView/CardView/XXXXView中.GridContro ...

  10. 北京Uber优步司机奖励政策(4月9日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...