Advanced Fruits (最大公共子序列的路径打印)
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.
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 (最大公共子序列的路径打印)的更多相关文章
- HDU 1503 Advanced Fruits(LCS+记录路径)
http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是 ...
- HDU-1053:Advanced Fruits(LCS+路径保存)
链接:HDU-1053:Advanced Fruits 题意:将两个字符串合成一个串,不改变原串的相对顺序,可将相同字母合成一个,求合成后最短的字符串. 题解:LCS有三种状态转移方式,将每个点的状态 ...
- Advanced Fruits(HDU 1503 LCS变形)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 最长公共子序列(加强版) Hdu 1503 Advanced Fruits
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- ZOJ 1076 Gene Assembly(LIS+路径打印 贪心)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=76 题目大意:在一个DNA上,给定许多基因的起始位置和结束位置,求出这 ...
- Advanced Fruits(好题,LCS的模拟)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1503:Advanced Fruits(动态规划 DP & 最长公共子序列(LCS)问题升级版)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1503 Advanced Fruits(最长公共子序列)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Advanced Fruits HDU杭电1503【LCS的保存】
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
随机推荐
- SQL Server 2012 - 数据更新操作
SELECT * FROM dbo.Student; --1, 插入数据 Insert ,逗号分隔可以同时插入多条 INSERT dbo.Student ( StuID, Class, StuName ...
- 跨域详解之-----Jsonp跨域
一.通过jsonp跨域 在js中,我们直接用XMLHttpRequest请求不同域上的数据时,是不可以的.但是,在页面上引入不同域上的js脚本文件却是可以的,jsonp正是利用这个特性来实现的. 比如 ...
- redis的主从配置
redis的主备配置比较简单,只需要在配置上新增slaveof属性即可,如果主节点需要密码验证,则在加上masterauth属性. 测试安装一个备用redis,备份前一章的节点redis的docker ...
- php的基础知识(三)
12.函数: 函数的功能: 定义:在真实的项目开发过程中,有些代码会重复利用,我们可以把它提出来,做成公共的代码,供团队来使用,这个我们封装的代码段,就是函数(功能). 优点: 1.提高代码的利用率. ...
- 『Python基础-13』函数 Function
这篇笔记记录的知识点: 函数的基本概念 自定义函数 函数的几种参数 编程的三种方式: 1.OOP 面向对象编程,万物皆对象,以class为主,抽象化 2.POP 面向过程编程,万事皆过程,def定义过 ...
- U盘kali系统安装
正言: 起初先百度了一下U盘安装Kali的资料,有很多版本和方法,当然还是以百度经验为例开始操作https://jingyan.baidu.com/article/cdddd41ca1027e53 ...
- Python 爬虫 (四)
requests: 练手 雪qiu网 import requests import json import re import pymysql url = 'https://xueqiu.com/v4 ...
- 运用busybox构建最小根文件系统
平台:vmware下ubuntu14.04前期准备:安装交叉编译环境arm-linux-gcc-4.5.1;下载完成BusyBox 1.23.2一.busybox构建1.make menuconfig ...
- java 递归打印20个斐波那契数
class Test { public static void main(String[] args) { // feibo j=new feibo(); for (int n = 1; n < ...
- macos 安装 brew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ...
递归方法打印路径,