最长公共子序列(加强版) Hdu 1503 Advanced Fruits
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
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.
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.
resulting fruit on one line. If more than one shortest name is possible, any one
is acceptable.
/*
例如 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的更多相关文章
- 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加强版)
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
- hdu 1503 Advanced Fruits
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 思路:这是一道最长公共子序列的题目,当然还需要记录路径.把两个字符串的最长公共字串记录下来,在递 ...
- 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(DP)
题意: 将两个英文单词进行合并.[最长公共子串只要保留一份] 输出合并后的英文单词. 思路: 求最长公共子串. 记录路径: mark[i][j]=-1:从mark[i-1][j]转移而来. mark[ ...
- caioj 1073 动态规划入门(三维一边推:最长公共子序列加强版(三串LCS))
三维的与二维大同小异,看代码. #include<cstdio> #include<cstring> #include<algorithm> #define REP ...
- 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 ...
随机推荐
- Jquery_操作cookies
首先引入jquery.cookie.js jquery.cookie.js下地址:http://plugins.jquery.com/cookie/ 操作文档: https://github.com/ ...
- Servlet处理get请求时的中文乱码问题
我们都知道,使用Servlet处理get请求时,如果get请求的参数中有中文,直接接收会是乱码,这个时候我们使用类似下面的语句来处理乱码: 12345 String name = request.ge ...
- SQL Server 数据库新建用户
1.在[安全性]中的[登录名]右键创建登陆用户 2. 输入相关信息 3. 然后去相应的数据库下[安全性]中右键添加用户 4.选择刚刚创建的用户名 5. 选择[默认架构]为 owner 6. 对[拥有的 ...
- CSS3初学篇章_6(自定义动画)
自定义动画 由于有一部分低版本的浏览器并不支持的问题,所以这个样式要多做兼容,各大浏览器兼容前缀如下: 前缀 浏览器 -webkit chrome和safari -moz firefox - ...
- rman恢复误删除的一张表(不完全恢复)
恢复误删除的一张表可以使用很多方法,如日志挖掘.闪回等,rman恢复(不完全恢复)肯定不是最好的,也不建议用, 现在我们只是演示一下这种恢复. 1 RMAN备份数据库 2创建测试表 3查看此时的SCN ...
- C#_项目做成安装包
首先打开项目-->点开解决方案右键单击添加-->新建项目-->找到其他项目类型(如下图)--> 点击确定-->单击应用程序文件夹-->在名称下的空白处单击--> ...
- Mysql和Oracle数据库concat()函数
一.函数的使用参数不同 Mysql支持多个字符串拼接: CONCAT(str1,str2,-) Oralce只支持两个字符串的拼接,若想拼接多个字符串可以嵌套使用concat CONCAT(str1, ...
- openfire配置MSSQL说明(数据库设置)
1.进入“SQL Server 配置管理器(SQL Server Configuration Manager)”,在左边窗口选择“SQL Server 2005网络配置”下面的分支“MSSQLServ ...
- IOS第16天(4,Quartz2D柱状图)
*** #import "HMBarView.h" #import "UIColor+Random.h" @implementation HMBarView - ...
- 关于JAVA中的String的使用与连接(转)
JAVA中的String连接性能 Java中的String是一个非常特殊的类,使它特殊的一个主要原因是:String是不可变的(immutable). String的不可变性是Ja ...