hdu 1503:Advanced Fruits(动态规划 DP & 最长公共子序列(LCS)问题升级版)
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
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 is terminated by end of file.
#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)问题升级版)的更多相关文章
- 动态规划之最长公共子序列LCS(Longest Common Subsequence)
一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- hdu 1503 Advanced Fruits(DP)
题意: 将两个英文单词进行合并.[最长公共子串只要保留一份] 输出合并后的英文单词. 思路: 求最长公共子串. 记录路径: mark[i][j]=-1:从mark[i-1][j]转移而来. mark[ ...
- HDU 1159 Common Subsequence【dp+最长公共子序列】
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 编程算法 - 最长公共子序列(LCS) 代码(C)
最长公共子序列(LCS) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定两个字符串s,t, 求出这两个字符串最长的公共子序列的长度. 字符 ...
- C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解
版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...
- 1006 最长公共子序列Lcs
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdks ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- 51Nod 1006:最长公共子序列Lcs(打印LCS)
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
随机推荐
- 查看Buffer Pool使用情况--[转]
----源自:微软官方博客论坛 我的SQL Server buffer pool很大,有办法知道是哪些对象吃掉我的buffer Pool内存么?比方说,能否知道是哪个数据库,哪个表,哪个index占用 ...
- 解决Html.CheckBoxFor中”无法将类型 bool 隐式转换为 bool。存在一个显式转换..."的方法
在后面加.Value属性 @Html.CheckBoxFor(m => m.IsComment.Value, new { style = "vertical-align: middle ...
- js加强小结
一)回顾JavaScript基础 (1)函数的定义方式 *>>正常方式 function add(num1,num2){...} >>构造器方式 var add = new F ...
- Linux防火墙的关闭和开启(转)
1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: service iptables sta ...
- 如何让iOS模拟器也能测试蓝牙4.0程序?
买一个CSR蓝牙4.0 USB适配器,插在Mac上 在终端输入sudo nvram bluetoothHostControllerSwitchBehavior="never" 重启 ...
- 10 分钟实现一个自己的server监控器
需求 近期须要给自己的server加入监控器.目的是监控server的内存.CPU.磁盘占用率,资源占用率过高的话能给自己发个提醒.当前主流的平台通常会提供邮件.短息.甚至会提供微信提醒,只是这类提醒 ...
- 整理 pandas 常用函数
1. df.head(n): 显示数据前n行,不指定n,df.head则会显示所有的行 2. df.columns.values获取所有列索引的名称 3. df.column_name: 直接获取列c ...
- js方式实现页面加遮罩效果
有时候在页面上执行查询的时候由于数据量很大,需要较长时间,所以就需要在等待结果期间不可以操作页面,那么可以使用如下代码给页面添加遮罩效果: $.messager.progress({ title: ' ...
- C++ virtual继承
C++ virtual继承的还有一种名称是菱形继承.主要目的是用于解决从不同类继承来的同名数据成员在内存中有不同的拷贝.造成数据不统一的问题,以致于在进行类释放时造成内存泄漏. 将共同的基类作为虚基类 ...
- CXF学习笔记 之 “注解”
@WebService 1.serviceName: 对外发布的服务名,指定 Web Service 的服务名称:wsdl:service.缺省值为 Java 类的简单名称 + Service.(字符 ...