Problem Description

The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn't work, but sometimes, in very rare cases, a new fruit emerges that tastes like a mixture between both of them. 
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

Each line of the input contains two strings that represent 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. 

Output

For 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
解题思路:题目的意思就是将给定的两个字符串合并成一个最短的新串,即要求它们的最长公共子序列在新串中只出现1次,并且新串的子序列中包含原来两个字符串。LCS的灵活应用。做法:假设第一个字符串为s1,第二个字符串为s2,在计算LCS同时,用一个二维数组标记当前i、j字符的相对状态:mp[i][j]=-1表示s1前i个字符中包含公共子序列的长度L1不小于s2前j个字符中的L2;mp[i][j]=0表示s1前i个字符中包含公共子序列的长度L1与s2前j个字符中的L2相等;mp[i][j]=1表示s1前i个字符包含公共子序列的长度L1小于s2前j个字符中的L2。接下来就可以利用递归的特性,通过对标记的判断,从后往前递归,回溯时就会根据标记依次输出其正确位置。解释一下递归部分的含义:如果当前mp[i][j]为-1,说明s1前i个字符中包含公共子序列的长度不小于s2前j个字符,并且此时s1[i]!=s2[j],即i>j,所以先输出s1[i],然后再在s1前i-1个字符中往前找,s2保持j位置不变(实际输出是在回溯时从前往后输出的,这样就比较好理解了),其他两种情况与此相同。注意:要初始化mp[i∈[1~len1]][0]=-1,mp[0][j∈[1,len2]]=1,含义与上面相同,为什么呢?举个栗子:假设递归到i=3,j=0时,由于一开始就将mp数组全部初始化为0,即mp[3][0]=0,这个时候满足第9行的条件,然后继续i-1=2,j-1=-1,此时已越界并且陷入死循环中,事实上初始为-1后即mp[3][0]=-1满足第8行这个条件,继续i-1=2,j=0不变递归下去就不会出错了,当i和j都为0时就可以开始回溯即return依次弹出栈顶信息并输出对应位置的字符。此题还有一个特点就是special judge特判表示答案不唯一,譬如样例三,新串还可以为peachr,其子序列同样包含原来两个字符串。
AC代码:
 #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加强版)的更多相关文章

  1. hdu 1503 Advanced Fruits(LCS输出路径)

    Problem Description The company "21st Century Fruits" has specialized in creating new sort ...

  2. HDU 1503 Advanced Fruits (LCS,变形)

    题意: 给两个水果名,要求他们的LCS部分只输出1次,其他照常输出,但是必须保持原来的顺序! 思路: 求LCS是常规的,但是输出麻烦了,要先求LCS,再标记两串中的所有LCS字符,在遇到LCS字符时, ...

  3. hdu 1503:Advanced Fruits(动态规划 DP & 最长公共子序列(LCS)问题升级版)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. 最长公共子序列(加强版) Hdu 1503 Advanced Fruits

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. hdu 1503 Advanced Fruits(最长公共子序列)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. hdu 1503 Advanced Fruits 最长公共子序列 *

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. HDU 1503 Advanced Fruits(LCS+记录路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是 ...

  8. HDU 1503 Advanced Fruits (LCS+DP+递归)

    题意:给定两个字符串,让你求一个最短的字符串,并且这个字符串包含给定的两个. 析:看到这个题,我知道是DP,但是,不会啊...完全没有思路么,我就是个DP渣渣,一直不会做DP. 最后还是参考了一下题解 ...

  9. hdu 1503 Advanced Fruits

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 思路:这是一道最长公共子序列的题目,当然还需要记录路径.把两个字符串的最长公共字串记录下来,在递 ...

随机推荐

  1. django 简易博客开发 5 markdown支持、代码高亮、gravatar头像服务

    上一篇博客介绍了comments库使用及ajax支持,现在blog已经具备了基本的功能,但是只能发表文字,不支持富文本编辑.今天我们利用markdown添加富文本支持. markdown语法说明: h ...

  2. Hibernate复习之Hibernate基本介绍

    众所周知.眼下流行的面向对象的对象关系映射的Java持久层框架有MyBatis和Hibernate.他们都是对象关系映射ORM. 解决的主要问题就是对象-关系的映射.域模型和关系模型都分别建立在概念模 ...

  3. IntelliJ 中类似于Eclipse ctrl+o的是ctrl+F12

    IntelliJ 中类似于Eclipse ctrl+o的是ctrl+F12 学习了:https://blog.csdn.net/sjzylc/article/details/47979815

  4. 一个重绘MFC的文件传输client

     一个重绘MFC的文件传输client,TCP/IP协议的文件传输client(支持上传.下载.续传.管理等功能,本处仅仅选择了上传功能).从用户视觉上看,肯定比原生MFC界面要有感觉,啥也不说了 ...

  5. Linux VPS/server上用Crontab来实现VPS自己主动化

    VPS或者server上常常会须要VPS或者server上常常会须要定时备份数据.定时运行重新启动某个服务或定时运行某个程序等等,一般在Linux使用Crontab,Windows以下是用计划任务(W ...

  6. JSP中的编译指令和动作指令的差别

    JSP中的编译指令和动作指令的差别 1.编译指令是通知Servlet引擎的处理消息.而动作指令仅仅是执行时的脚本动作 2.编译指令是在将JSP编译成Servlet时起作用,而动作指令可替换成JSP脚本 ...

  7. LoaderManager使用具体解释(四)---实例:AppListLoader

    实例:AppListLoader 这篇文章将是我的第四篇,也就是最后一篇该系列的文章.请在评论里面告诉我他们是否实用.前面几篇文章的链接例如以下: 一:Loaders之前世界 二:了解LoaderMa ...

  8. iOS项目开发实战——plist数组解析

    plist数据是苹果公司创造的数据格式,基于XML,因为在iOS,Mac系统中操作plist很方便,所以我们经常会用到.在iOS项目中.系统会自己主动生成一个Info.plist文件,里面存放了iOS ...

  9. 理解Paxos Made Practical

    Paxos Made Practical 当一个组中一台机器提出一个值时,其它成员机器通过PAXOS算法在这个值上达成一致. Paxos分三个阶段. 第一阶段: 提出者会选出一个提议编号n(n> ...

  10. 为Html.EditorForModel自定义模版

    对于MVC视图渲染来说,大家应该不会陌生,但对于模型的渲染,不知道是否听说过,主要是说Model通过它属性的相关特性(DataType,UIHint)来将它们自动渲染到View上,这是一个比较不错的技 ...