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. 简易坦克大战python版

      #! /usr/bin/env python # -*- coding:utf8 -*- ''' *author:wasua *purpose:学习python语言,其中的类以及pygame应用 ...

  2. 开发和调试第一个 LLVM Pass

    1. 下载和编译 LLVM LLVM 下载地址 http://releases.llvm.org/download.html,目前最新版是 6.0.0,下载完成之后,执行 tar 解压 llvm 包: ...

  3. go加密算法:CBC对称加密(一)--3DES/AES

    其实对称加密中的:DES\3DES\AES 采取的加解密步骤一致,只是小的细节不太一样.大家多看看就能写出来了 // rsao1.go package main import ( "byte ...

  4. Java : Netty 入门案例

    接收端代码: public class IOServer { public static void main(String[] args) throws IOException, Interrupte ...

  5. 使用PHPExcel 读取 表格数据, 发现中文全变成 FALSE??

    出现这样的情况, 你可以看看你的表格是不是 CSV 格式的. 如果是, 那就赶紧另保存为 xls.xlsx 等格式的表格 . 因为 PHPExcel 对 Csv 的表格不感冒....

  6. consonant_摩擦音_咬舌音

    consonant_摩擦音_咬舌音_[θ]和[ð].[h] 咬舌音:咬住舌尖发音. [θ]:牙齿咬住舌尖,送气,气流摩擦发出声音,声带不振动: faith.thank.healthy.both.th ...

  7. 笔记-django第一个项目

    笔记-django第一个项目 1.      创建项目 安装 Django 之后,现在有了可用的管理工具 django-admin.可以使用 django-admin 来创建一个项目: 看下djang ...

  8. Lucene第二讲——索引与搜索

    一.Feild域 1.Field域的属性 是否分词:Tokenized 是:对该field存储的内容进行分词,分词的目的,就是为了索引. 否:不需要对field存储的内容进行分词,不分词,不代表不索引 ...

  9. 【转】odoo学习之:Environment

    Environment类提供了对ORM对象的封装,同时提供了对注册类的访问,记录集的缓存,以及管理重计算的数据结构. 对于继承了Model的类来说可以直接通过self.env对Environment进 ...

  10. maven私有库搭建

    一.在企业中基本上都会有自己的maven私有库,主要的目的就是方便依赖包的下载.如果采用远程的方式来实现的话,很多时候会考虑网速问题.如果自己活着公司搭建的私有库,这一样在使用上面会效率更高. 二.私 ...