最长公共子序列(加强版) 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 ...
随机推荐
- Flex——Array,ArrayCollection,Vector性能比较(转)
测试方法 private function Test():void { ;j<;j++) { trace("插入10000项============"); var t1:in ...
- Equal Sum Sets
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49406 题意: 输入n,k,s,求在不小于n的数中找出k个不同的数 ...
- 让Xcode的 stack trace信息可读
让Xcode的 stack trace信息可读 昨天在写 iOS 代码的时候,调试的时候模拟器崩溃了.异常停在了如下整个 main 函数的入口处: int main(int argc, char *a ...
- html5 语义
页面示意图
- HDU3732 背包DP
Ahui Writes Word Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 2577
How to Type Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Lua迭代器和泛型for
1.迭代器与closure 在lua中,迭代器通常为函数,每调用一次函数,会返回集合中的下一个元素.每个迭代器在成功调用的时候,都需要保存一些状态,closure(闭包)完美为迭代器运用而生. fun ...
- centos6.6 LVS+keepalived
之前有写过keepalived+mysql 和lvsDR模式的分析篇.然而LVS没有写高冗余.今天来写一篇LVS+keepalived的 LVSDR只负责转发,LVS也没有nginx后端检查功能,所 ...
- JSP页面转向方式
1.RequestDispatcher.forward() 是在服务器端起作用,当使用forward()时,Servlet engine传递HTTP请求从当前的Servlet or JSP到另外一个S ...
- BizTalk 开发系列(四十二) 为BizTalk应用程序打包不同的环境Binding
我们在使用微软或者其他公司提供的BizTalk应用程序MSI包的时候经常会有一个目标环境的选择选项.该选项可以在不同的环境下使用不同的绑定(BizTalk应用程序配置)感觉很高级. 其实这个非常的简单 ...