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. hashcode和equals区别

    hashcode:对象的初始地址的整数表示 Java中的对象是JVM在管理,JVM会在她认为合适的时候对对象进行移动,比如,在某些需要整理内存碎片的GC算法下发生的GC.此时,对象的地址会变动,但ha ...

  2. ATM购物作业

    一. 基本需求 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录 支持账户间转账 记录日常消费 ...

  3. shell脚本实现目录的“5S”作业

    shell,又称为命令解释器.首先它是一个软件,有很多个版本,现在最流行的为bash,它作为用户和内核沟通的中间桥梁,在系统中起着举足轻重的作用 shell脚本,是一个以.sh结尾的文件,里面是诸如l ...

  4. DataSet和泛型之间相互转换

    取数据的时候,存储过程返回了多个结果集,后台用DataSet去接收这几个结果集,然后接收之后,需要将结果集转换为不同的实体,于是下面的代码便出现了. /// <summary> /// 将 ...

  5. 01-http简介-四层 七层 三次握手

    HTTP简介.请求方法与响应状态码 接下来想系统的回顾一下TCP/IP协议族的相关东西,当然这些东西大部分是在大学的时候学过的,但是那句话,基础的东西还是要不时的回顾回顾的.接下来的几篇博客都是关于T ...

  6. margin中的bug解决方法

    margin bug问题 : 当做子元素中使用margin-top: 50px;父子元素都会跑出50px, 解决方法: 在父元素中使用下面三种任意一种都可以. 方法一:给父元素加边框 border: ...

  7. 离不开的微服务架构,脱不开的RPC细节(值得收藏)!!!

    服务化有什么好处? 服务化的一个好处就是,不限定服务的提供方使用什么技术选型,能够实现大公司跨团队的技术解耦,如下图所示: 服务A:欧洲团队维护,技术背景是Java 服务B:美洲团队维护,用C++实现 ...

  8. flume搭建新手测试环境

    硬件环境: 腾讯云,两台服务器8G 双核 软件环境: flume1.8.jdk1.8,centos6 第一次搭建也是各种找文件,只知道flume是日志抓取服务,也听说了非常稳定强大的服务,正好公司需要 ...

  9. spark----词频统计(一)

    利用Linux系统中安装的spark来统计: 1.选择目录,并创建一个存放文本的目录,将要处理的文本保存在该目录下以供查找操作: ① cd /usr/local ②mkdir mycode ③ cd ...

  10. c指针学习

    #include <stdio.h> void main() { int a1=10; int a2=11; int * pa1, * pa2; pa1 = &a1; pa2 = ...