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 思路:这是一道最长公共子序列的题目,当然还需要记录路径.把两个字符串的最长公共字串记录下来,在递 ...
随机推荐
- JMeter学习non-gui模式运行
-h, --help print usage information and exit #打印帮助信息 -v, --version print the version information and ...
- js根据ip自动获取地址(省市区)
HTML: <html> <head> <meta charset="utf-8"> <meta name="viewport& ...
- 解决多人开发时使用window.onload的覆盖问题
通用型小函数:解决多人开发时,同时使用window.onload事件所出现的后面的window.onload函数覆盖前面一个window.onload函数的问题. function addLoadEv ...
- Azure系列2.1 —— com.microsoft.azure.storage.blob
网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习心得做下笔记,文中不正确地方请大家指正. Azure Blob ...
- [转帖]SQL Server 索引中include的魅力(具有包含性列的索引)
SQL Server 索引中include的魅力(具有包含性列的索引) http://www.cnblogs.com/gaizai/archive/2010/01/11/1644358.html 上个 ...
- CMake--模块的使用和自定义模块
1.链接外部库 如果程序中使用了外部库,事先并不知道它的头文件和链接库的位置,就要给出头文件和链接库的查找方法,并将他们链接到程序中. FIND_PACKAGE(<name> [major ...
- ssl证书部署问题
问:我现在得到的ssl证书是.crt和.key两个在nginx环境下部署的证书,如果我们改用是tomcat,现在把这两个文件合成了.jks给tomcat使用,合成的时候输入的jks密码是不是就是部署在 ...
- JDK 12 & JAVA
JDK 12 & JAVA js style https://github.com/winterbe https://winterbe.com/posts/2018/09/24/java-11 ...
- 销售合同金额数据从Excel导入
一.业务需求 1.新增了销售合同金额的字段,但是老数据没有这个字段:所以销售合同金额从销售合同附件的各品种金额之和. 2.制作好excel字段模板,将此模板发送给销售业务部门来统计并完成excel表格 ...
- LR 场景选项配置--笔记
1 tools-options --设置关系到loadgenerator行为应用于一个场景中的所有的load generator 这些设置用于未来所有运行的场景并且通常只需要设置一次 2 expert ...