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 ...
随机推荐
- 自动曝光修复算法 附完整C代码
众所周知, 图像方面的3A算法有: AF自动对焦(Automatic Focus)自动对焦即调节摄像头焦距自动得到清晰的图像的过程 AE自动曝光(Automatic Exposure)自动曝光的是为了 ...
- centos7.3上编译安装percona5.7.18
一,删除操作系统自带mariadb yum remove mariadb 二,下载需要的安装包 percona-toolkit-3.0.3-1.el7.x86_64.rpm boost_1_59_0. ...
- Linux查看MAC地址方法
注:一般默认的网卡文件名是eth0,根据IP地址对应的实际情况区判断是ethx即可. 1. ip -a . cat /sys/class/net/ens39/address 其中 HWaddr字段就 ...
- netfilter 学习摘要
netfilter 子系入口在L3,完成后把数据包发往L4 netfilter 主要功能: 数据包选择(iptables) 数据包过滤 网络地址转换(NAT) 数据包操纵(在路由选择之前或之后修改数据 ...
- Linux及FL2440使用过程遇到的各种问题和小技巧
原文链接:http://www.cnblogs.com/NickQ/p/8900474.html ## Linux及FL2440使用过程遇到的各种问题和小技巧 关于移植linux根文件系统中的问题 在 ...
- Python学习手册之类和继承
在上一篇文章中,我们介绍了 Python 的函数式编程,现在我们介绍 Python 的类和继承. 查看上一篇文章请点击:https://www.cnblogs.com/dustman/p/100106 ...
- fiddler响应报文的headers属性详解
fiddler响应报文的headers属性详解 (1)Cache头域 1. Cache-Control 在请求报文已经说过了,用于设置缓存的属性,浏览内容不被缓存. 2. Data 生成消息的具体时间 ...
- Django模板语言与视图(view)
常用语法 {{ }}和{% %} 变量相关的用{{}} , 逻辑相关的用{% %} 变量 在Django的模板语言中按此语法使用:{{ 变量名 }}. 当模版引擎遇到一个变量,它将计算这个变量,然后 ...
- Go中处理文本格式
首先是xml 解析xml package main import ( "encoding/xml" //xml标准库 "fmt" "io/ioutil ...
- 主存和cache的地址映射
cache是一种高速缓冲寄存器,是为解决CPU和主存之间速度不匹配而采用的一项重要技术. 主存与cache的地址映射方式有全相联方式.直接方式和组相联方式三种. 直接映射(directmapping) ...
递归方法打印路径,