hdu 1503 Advanced Fruits(LCS输出路径)
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 is terminated by end of file.
#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int dp[][];
int mark[][]; //0表示为公共字母 1表示i-1没有贡献 -1表示j-1没有贡献
string s,t;
void output(int i,int j){ //打印路径
// cout<<i<<" "<<j<<endl;
if(i==||j==){
if(i==)
for(int k=;k<j;k++)
cout<<t[k];
else
for(int k=;k<i;k++)
cout<<s[k];
return ;
}
if(mark[i][j]==){
output(i-,j-);
cout<<s[i-];
}else if(mark[i][j]==-){
output(i-,j);
cout<<s[i-];
}else{
output(i,j-);
cout<<t[j-];
}
}
int main(){
ios::sync_with_stdio(false);
while(cin>>s>>t){
memset(dp,,sizeof(dp));
memset(mark,,sizeof(mark));
int len1,len2;
len1=s.length(); len2=t.length();
for(int i=;i<=len1;i++)
for(int j=;j<=len2;j++){
if(s[i-]==t[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]=-;
}
}
output(len1,len2);
cout<<endl;
}
return ;
}
hdu 1503 Advanced Fruits(LCS输出路径)的更多相关文章
- 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加强版)
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
- HDU 1503 Advanced Fruits (LCS+DP+递归)
题意:给定两个字符串,让你求一个最短的字符串,并且这个字符串包含给定的两个. 析:看到这个题,我知道是DP,但是,不会啊...完全没有思路么,我就是个DP渣渣,一直不会做DP. 最后还是参考了一下题解 ...
- hdu 1503 Advanced Fruits
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 思路:这是一道最长公共子序列的题目,当然还需要记录路径.把两个字符串的最长公共字串记录下来,在递 ...
随机推荐
- MySQL数据类型优化—整数类型优化选择
原文:http://bbs.landingbj.com/t-0-240002-1.html 在设计数据库的时候,整数类型的使用时不可避免的如ID,类型等. 在选择整数的同时主要是考虑是数据范围,如是否 ...
- java不同的包下相同的类名的问题与解决办法
Java中的类以包进行分类组织,当程序中需要用到某个包下的类时,可以以该类的全限定名进行引用.这样,不同的包中的类就可以同名,不会产生混淆. 但是这样就可能导致引用的时候会产生一些问题. 第一个问题, ...
- 从主机A得到id_rsa.pub文件,在主机B创建用户danny加入该文件,实现主机A免密登录主机B
操作步骤: 1.主机A 生成公钥id_rsa.pub文件并导出该文件 root@mytest:~# ssh-keygen Generating public/private rsa key pair. ...
- IWMS后台上传文章,嵌入视频,调用优酷通用代码
<a href="http://player.youku.com/player.php/sid/XODcxNjM3OTYw/v.swf " target="_bla ...
- Jackson将对象转换为json字符串时,设置默认的时间格式
maven需要的依赖: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifac ...
- PHPStorm 配置命名空间
文件-设置-Directories 选中:application 点击顶部:Sources,右侧会出现 Source Floders 配置项 点击:p进行设置 输入app\
- How to create DMG on macOS
hdiutil create -srcfolder /users/test1/ -volname test1 /users/test/test1.dmg
- Nginx proxy_protocol协议
L:113
- mysql语句-DML语句
DML语句 DML是指对数据库中表记录的操作,主要包括数据的增删改查以及更新,下面依次介绍 首先创建一张表:: 表名:emp 字段:ename varchar(20),hiredate date ,s ...
- 解决Maven依赖报错问题
问题现象:从svn上签下源代码,本地执行打包命令,提示如下错误: [ERROR] Failed to execute goal on project dddwriter: Could not reso ...