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. javascript 面向对象编程(工厂模式、构造函数模式、原型模式)

      javascript 面向对象编程(工厂模式.构造函数模式.原型模式) CreateTime--2018年3月29日17:09:38 Author:Marydon 一.工厂模式 /** * 工厂模 ...

  2. 18-spring学习-利用Annotation配置AOP

    之前是通过配置完成aop操作,如果自己写的话,太麻烦了,可以使用基于annotation的配置完成. 第一步:打开AOP的annotation支持 加上一句话: <context:annotat ...

  3. 具有SSM框架的CRUD与多条件查询

    概述 居于ssm版本的crud跟多添加查询, 并带分页的demo 详细 代码下载:http://www.demodashi.com/demo/13653.html 一.功能展示 部门CRUD: 员工C ...

  4. Solr控制覆盖或覆写索引开关

    http://wiki.apache.org/solr/UpdateXmlMessages#Optional_attributes_for_.22field.22 ptional attributes ...

  5. npoi IWorkbook HSSFWorkbook XSSFWorkbook 拥有 IEnumerator GetEnumerator(); 方法 可以遍历workbook 每个元素为每个sheet页

  6. struts2开发流程及配置,域对象对数据存储的3种方式

    一.开发流程 1)引入 jar 包,其中必须引入的有(我是用的struts是2.3.32) commons-fileupload-1.3.2.jar     |文件上传下载commons-io-2.2 ...

  7. 常用的SQL

    --时间计算: select GETDATE() ,GETDATE()) ,GETDATE()) ,GETDATE()) ,GETDATE()) ,GETDATE()) ,GETDATE()) --查 ...

  8. 禁止apache显示目录索引 apache禁止列目录

    禁止Apache显示目录索引的常见的3种方法. 要实现禁止Apache显示目录索引,只需将Option中的Indexes去掉即可. 禁止Apache显示目录索引,禁止Apache显示目录结构列表,禁止 ...

  9. (oneway void) release中oneway的意思

    oneway is used with the distributed objects API, which allows use of objective-c objects between dif ...

  10. 在CentOS上安装部署MooseFS分布式文件系统

    参考资料: http://www.moosefs.org/tl_files/manpageszip/moosefs-step-by-step-tutorial-cn-v.1.1.pdf 环境介绍:OS ...