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 思路:这是一道最长公共子序列的题目,当然还需要记录路径.把两个字符串的最长公共字串记录下来,在递 ...
随机推荐
- 什么是arp协议?
转自:https://blog.csdn.net/tigerjibo/article/details/7351992 ARP (Address Resolution Protocol) 是个地址解析协 ...
- html5-attr和prop
###1.什么是attrbute和property attribute(attr) html标签的预定义属性 checked html标签的自定义属性 a eg: <input ...
- CMake--静态库与动态库构建
小结内容 建立一个静态库和动态库,提供 HelloFunc 函数供其他程序编程使用, HelloFunc 向终端输出Hello World 字符串. 安装头文件与共享库. 1.代码与CMakeList ...
- zTree树形菜单交互选项卡效果实现
1. 添加自定义属性 page 2. 为 ztree 每个树形节点,添加点击事件 <!DOCTYPE html> <html> <head> <meta ch ...
- 金蝶CLOUD与EAS的区别
1.金蝶K/3 WISE主要面向单体制造企业(主要是离散制造企业):2.金蝶K/3 Cloud主要面向业务类型单一(即主营业务单一)的.注重供应链与生产业务协同的.中小型(二层集团??)集团性企业(主 ...
- hadoop 管理命令dfsadmin
hadoop 管理命令dfsadmin dfsadmin 命令用于管理HDFS集群,这些命令常用于管理员. 1. (Safemode)安全模式 动作 命令 把集群切换到安全模式 bin/hdfs df ...
- hadoop分布式系统架构详解
hadoop 简单来说就是用 java写的分布式 ,处理大数据的框架,主要思想是 “分组合并” 思想. 分组:比如 有一个大型数据,那么他就会将这个数据按照算法分成多份,每份存储在 从属主机上,并且在 ...
- freemarker 简单操作
操作字符串函数 1. substring(start,end)从一个字符串中截取子串 start:截取子串开始的索引,start必须大于等于0,小于等于end end: 截取子串的长度,end必须大于 ...
- python之路--操作系统介绍,进程的创建
一 . 操作系统的作用: 1:隐藏丑陋复杂的硬件接口,提供良好的抽象接口 2:管理.调度进程,并且将多个进程对硬件的竞争变得有序 二 多道技术: 所谓多道程序设计技术,就是指允许多个程序同时进入内存 ...
- solr安装配置(一)
本文使用的solr版本是solr-5.5.5. 步骤: 1.解压solr压缩包. 2.将solr-5.5.5\server\solr-webapp目录下面的文件拷贝到Tomcat的webapps目录下 ...