Advanced Fruits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1360    Accepted Submission(s): 672
Special Judge

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
Source
 
Recommend
linle   |   We have carefully selected several similar problems for you:  1502 1501 1227 1080 1158  

 
   这是第二次坑在这道题上了,试验了好几种思路不就是运行时错误就是WA,最后火了还是看了别人的博客才做出来。
  思路很简单,在LCS的基础之上加上路径记录。生成dp数组的时候做上标记,之后按顺序输出结果字符串。
  我坑就坑在我没有想到好的记录路径的方法,还是高手的方法精炼,准确又简单。
  下面是代码,注意数组的初始化。
 
 #include <iostream>
#include <string.h>
using namespace std;
int dp[][];
int f[][];
char a[],b[];
int l1,l2;
int Length(char a[]) //返回一字符串长度
{
int i;
for(i=;a[i]!='\0';i++);
return i;
}
void lcs_pre(char a[],char b[]) //进行标记
{
l1 = Length(a);
l2 = Length(b);
for(int i=;i<=l1;i++){
dp[i][] = ;
f[i][] = ;
}
for(int i=;i<=l2;i++){
dp[][i] = ;
f[][i] = ;
}
for(int i=;i<=l1;i++)
for(int j=;j<=l2;j++){
if( a[i-]==b[j-] ){
dp[i][j] = dp[i-][j-] + ;
f[i][j] = ;
}
else if( dp[i][j-]>=dp[i-][j] ){
dp[i][j] = dp[i][j-];
f[i][j] = ;
}
else{
dp[i][j] = dp[i-][j];
f[i][j] = ;
}
}
}
void Print(int x,int y) //输出结果字符串
{
if(x== && y==)
return ;
switch(f[x][y]){
case :
Print(x,y-);
cout<<b[y-];
break;
case :
Print(x-,y-);
cout<<a[x-];
break;
case :
Print(x-,y);
cout<<a[x-];
break;
default:break;
}
return ;
}
int main()
{
while(cin>>a>>b){
l1 = Length(a);
l2 = Length(b);
lcs_pre(a,b); //进行标记
Print(l1,l2);
cout<<endl;
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 1503:Advanced Fruits(动态规划 DP & 最长公共子序列(LCS)问题升级版)的更多相关文章

  1. 动态规划之最长公共子序列LCS(Longest Common Subsequence)

    一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...

  2. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  3. hdu 1503 Advanced Fruits(DP)

    题意: 将两个英文单词进行合并.[最长公共子串只要保留一份] 输出合并后的英文单词. 思路: 求最长公共子串. 记录路径: mark[i][j]=-1:从mark[i-1][j]转移而来. mark[ ...

  4. HDU 1159 Common Subsequence【dp+最长公共子序列】

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. 编程算法 - 最长公共子序列(LCS) 代码(C)

    最长公共子序列(LCS) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定两个字符串s,t, 求出这两个字符串最长的公共子序列的长度. 字符 ...

  6. C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解

    版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...

  7. 1006 最长公共子序列Lcs

    1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdks ...

  8. POJ 1458 Common Subsequence(最长公共子序列LCS)

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  9. 51Nod 1006:最长公共子序列Lcs(打印LCS)

    1006 最长公共子序列Lcs  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...

随机推荐

  1. if-else用法

      CreateTime--2016年10月31日14:22:25Author:Marydonif-else的多种用法: //方式一 function test1 (t) { var bl = t | ...

  2. js与jQuery实现方式对比汇总

      CreateTime--2016年12月16日09:11:23Author:Marydonjs与jQuery实现方式对比汇总 <div id="ListContainer" ...

  3. Java中被你忽视的四种引用(转)

    转载自:http://blog.csdn.net/u010425776/article/details/50760053 Java的数据类型分为两类:基本数据类型.引用数据类型. 基本数据类型的值存储 ...

  4. 基于canvas的原生JS时钟效果

    概述 运用html5新增画布canvas技术,绘制时钟效果,无需引用任何插件,纯js. 详细 代码下载:http://www.demodashi.com/demo/11935.html 给大家介绍一个 ...

  5. C# 通过form表单下载文本文件

    public void DownLoadConfigFile(string name) { //获取文件字符串内容 var data = _service.ReadFileStr(_configure ...

  6. 【Linux】centos和ubuntu下php5安装redis2.24扩展

    1.服务器先安装redis-server,这是毋庸置疑的!!! 2.服务器开启redis-server,配置相关参数 3.配置好redis服务器后,再安装php的redis扩展phpredis. 一. ...

  7. 【转帖】Sigma水平和缺陷率的对应关系:正态分布中心和1.5标准差偏移

    http://www.pinzhi.org/thread-5395-1-1.html Sigma水平和缺陷率的对应关系:正态分布中心和有1.5个标准差偏移 在过程稳定时,若给出了规范限,过程的平均与标 ...

  8. Oracle相关操作示例(导出导入dmp需要采用)

    删除用户:drop user pnxd cascade; 导出数据:exp pnxd/padmin@A file=c:\bb.dmp full=y 导入数据:imp pnxd/padmin@PNXD ...

  9. Altium PCB布局时快速摆放元件的技巧

    http://www.openedv.com/posts/list/45238.htm pcb窗口:工具--->交叉选择模式

  10. SQLyog之MySQL客户端的下载、安装和使用(旗舰版)(推荐)

    不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号:   大数据躺过的坑      Java从入门到架构师      人工智能躺过的坑          ...