Advanced Fruits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1426    Accepted Submission(s):
719
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
 
 
这道题也是关于最长公共子序列的问题,但是,需要将公共子序列标记一下。刚开始做的时候想到是要求最长公共子序列,但是不知道怎么有效地将结果输出来,后来想想,只能将两个数组从后往前扫描一遍,将公共子序列单独处理,保存到另一个数组c中,最后将数组c倒序输出。
 /*
例如 apple peach
p e a c h
0 0 0 0 0 0
a 0 0 0 1 1 1
p 0 1 1 1 1 1
p 0 1 1 1 1 1
l 0 1 1 1 1 1
e 0 1 2 2 2 2
*/
#include<iostream>
#include <cstring>
using namespace std;
#define MAX 105
char ch1[MAX],ch2[MAX],ch[MAX];
int dp[MAX][MAX];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int i,j,k;
int m,n;
while(cin.getline(ch1,MAX,' ') && cin.getline(ch2,MAX,'\n'))
{
m = strlen(ch1);
n = strlen(ch2);
for(i=;i<=n;i++)
dp[][i] = ;
for(j=;j<=m;j++)
dp[j][] = ;
for(i=;i<=m;i++)
for(j=;j<=n;j++)
if(ch1[i-] == ch2[j-])
dp[i][j] = dp[i-][j-] + ;
else
dp[i][j] = max(dp[i-][j],dp[i][j-]);
//以上是求最长公共子序列
i = strlen(ch1),j = strlen(ch2);
k=;
while(i!= || j!=) //将所求的序列保存在数组ch中,从后往前依次比较两个数组
{
if(i==) //说明数组ch2还有剩余元素
{
ch[k++] = ch2[j-];
j--;
continue;
}
else if(j==) //说明数组ch1还有剩余元素
{
ch[k++] = ch1[i-];
i--;
continue;
}
else if(ch1[i-] != ch2[j-])
{
if(dp[i][j] == dp[i][j-])
{
ch[k++] = ch2[j-];
j--;
continue;
}
else if(dp[i][j] == dp[i-][j])
{
ch[k++] = ch1[i-];
i--;
continue;
}
}
else
{
ch[k++] = ch1[i-];
i--;j--;
continue;
}
}
for (i=k-;i>=;i--)
cout<<ch[i];
cout<<endl;
memset(ch1,,sizeof(ch1));
memset(ch2,,sizeof(ch2));
}
return ;
}

最长公共子序列(加强版) Hdu 1503 Advanced Fruits的更多相关文章

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

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

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

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

  3. 题解报告:hdu 1503 Advanced Fruits(LCS加强版)

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

  4. hdu 1503 Advanced Fruits

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

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

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

  6. hdu 1503 Advanced Fruits(DP)

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

  7. caioj 1073 动态规划入门(三维一边推:最长公共子序列加强版(三串LCS))

    三维的与二维大同小异,看代码. #include<cstdio> #include<cstring> #include<algorithm> #define REP ...

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

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

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

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

随机推荐

  1. UICollectionView集合视图的概念

    如何创建UICollectionView 集合视图的布局UICollectionViewFlowLayout 自定义cell 布局协议UICollectionViewDelegateFlowLayou ...

  2. CentOS 6.7安装Mysql 5.7

    Step1: 检测系统是否自带安装mysql # yum list installed | grep mysql Step2: 删除系统自带的mysql及其依赖命令: # yum -y remove ...

  3. 【android design】android常用设计资源

    一.概述 大部分程序员擅长开发,但是对于设计却知之甚少.这直接导致,程序员在初期开发出来的应用(大多为兴趣或实用导向)中看不中用.因此,有必要搜集整合一些设计资源,这样既能减轻程序员在设计上所耗费的时 ...

  4. Hibernate+Struts2进行数据的修改

    1.先把userid传给修改的页面 2.跳转到修改的页面 3.用request接收传入输入需改信息的页面 传到action Action,  通过request获取id service层 DAO层 & ...

  5. Lamp下安全配置随笔

    Apache方面: 1.apache有两个指令可以输出服务器的细节,即ServerSignature和ServerTokens. 当这两个指令一起使用时,会输出apache的版本号,php的版本号,i ...

  6. C# 文件和文件夹操作

    一.文件操作 1.File类的常用静态方法: void AppendAllText(string path, string contents),将文本contents附加到文件path中 bool E ...

  7. JS的Touch事件们,触屏时的js事件

    丫的,终于找到了JS在平板电脑上的事件!!!   iphone.ipod Touch.ipad触屏时的js事件   1.Touch事件简介   pc上的web页面鼠标会产生onmousedown.on ...

  8. centos重启不能自动联网的解决方法

    在命令行下输入 下面的ifcfg-eth0,eth0为我的网卡名字.机器之间不同,请先查看自己网卡的名字 vi /etc/sysconfig/network-scripts/ifcfg-eth0 进行 ...

  9. 测试的程序 test.php,保存放IIS的根目录下

    IIS+PHP的配置的方法,试过之后很多都不能达到效果.于是总结了大部分的文章后就得出了这样的方法 一.下载必须的程序:(1) 先到PHP的官方网站下载一个PHP(本文就以PHP 4.4.2为例).网 ...

  10. 通过Queue的构造函数的可选参数maxsize来设定队列长度

    创建一个"队列"对象 import Queuemyqueue = Queue.Queue(maxsize = 10) Queue.Queue类即是一个队列的同步实现.队列长度可为无 ...