题解报告:hdu 1503 Advanced Fruits(LCS加强版)
Problem Description
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.
Input
Input is terminated by end of file.
Output
Sample Input
Sample Output
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
char s1[maxn],s2[maxn];
int len1,len2,dp[maxn][maxn],mp[maxn][maxn];
void print_LCS(int i,int j){
if(!i&&!j)return;//如果i,j都为0,则直接返回
else if(mp[i][j]==-){print_LCS(i-,j);putchar(s1[i]);}
else if(mp[i][j]==){print_LCS(i-,j-);putchar(s1[i]);}
else {print_LCS(i,j-);putchar(s2[j]);}
}
int main(){
while(~scanf("%s%s",s1+,s2+)){
memset(dp,,sizeof(dp));
memset(mp,,sizeof(mp));//初始状态默认两个字符串是相等的
len1=strlen(s1+),len2=strlen(s2+);
for(int i=;i<=len1;++i)mp[i][]=-;
for(int j=;j<=len2;++j)mp[][j]=;
for(int i=;i<=len1;++i){
for(int j=;j<=len2;++j){
if(s1[i]==s2[j])dp[i][j]=dp[i-][j-]+;
else if(dp[i-][j]>=dp[i][j-])dp[i][j]=dp[i-][j],mp[i][j]=-;
else dp[i][j]=dp[i][j-],mp[i][j]=;
}
}
print_LCS(len1,len2);
puts("");
}
return ;
}
题解报告:hdu 1503 Advanced Fruits(LCS加强版)的更多相关文章
- hdu 1503 Advanced Fruits(LCS输出路径)
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
- HDU 1503 Advanced Fruits (LCS,变形)
题意: 给两个水果名,要求他们的LCS部分只输出1次,其他照常输出,但是必须保持原来的顺序! 思路: 求LCS是常规的,但是输出麻烦了,要先求LCS,再标记两串中的所有LCS字符,在遇到LCS字符时, ...
- 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 ...
- hdu 1503 Advanced Fruits(最长公共子序列)
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) ...
- HDU 1503 Advanced Fruits(LCS+记录路径)
http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是 ...
- HDU 1503 Advanced Fruits (LCS+DP+递归)
题意:给定两个字符串,让你求一个最短的字符串,并且这个字符串包含给定的两个. 析:看到这个题,我知道是DP,但是,不会啊...完全没有思路么,我就是个DP渣渣,一直不会做DP. 最后还是参考了一下题解 ...
- hdu 1503 Advanced Fruits
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 思路:这是一道最长公共子序列的题目,当然还需要记录路径.把两个字符串的最长公共字串记录下来,在递 ...
随机推荐
- eclipse中maven插件上传项目jar包到私服
我们知道,每一个公司都会有自己的工具包或公共包.这样的包就能够上传到公司的maven私服,就不用每一个人都去同步开发包了. 那么,怎么把本地项目打包并公布到私服呢?依照例如以下步骤就能够轻松完毕. 1 ...
- 【网络】TCP的流量控制
一.利用滑动窗口实现流量控制 流量控制是让发送方的发生速率不要太快,要让接收方来得及接收. 发送方的发送窗口不能超过接收方给出的接收窗口的数值,TCP的窗口单位是字节,不是报文段. TCP为每一个连接 ...
- 2003 -Can't connection to mysql server on | navicat for mysql Access denied for user 'root'@''ip'(using password :yes)
用本机windows上的Navicat for mysql链接虚拟机Linux的mysql数据库时,第一次连接的时候报的错误是 2003 -Can't connection to mysql serv ...
- VM虚拟机ping不通局域网其他主机的解决办法
1 我的笔记本的无线网卡是自动获取IP,并且是通过无线网卡上网. 2 我的有线网卡是通过自己设定IP跟局域网的其他机器连通.当前设定的IP为172.16.17.2 3我需要连接的局域网另一个主 ...
- leetCode(26):Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 在调试状态查看DateTable里的数据信息
- 2.7 xargs和exec详解【转】
本文转载自:http://ask.apelearn.com/question/13323 常用在查找中exec主要是和find一起配合使用,而xargs就要比exec用的地方要多了. exec 应用 ...
- 什么叫强类型的DATASET
强类型DataSet,是指需要预先定义对应表的各个字段的属性和取值方式的数据集.对于所有这些属性都需要从DataSet, DataTable, DataRow继承,生成相应的用户自定义类.强类型的一个 ...
- Multi-threading Android Apps for Multi-core Processors – Part 1 of 2
Can my single-threaded application benefit from multiple cores? How? Even a single-threaded applicat ...
- 并不对劲的bzoj5020:loj2289:p4546:[THUWC2017]在美妙的数学王国中畅游
题目大意 有一个n(\(n\leq 10^5\))个点的森林,每个点\(u\)上有个函数\(f_u(x)\),是形如\(ax+b\)或\(e^{ax+b}\)或\(sin(ax+b)\)的函数,保证当 ...