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的最长公共子序列(子序列不要求是连续的). ...
随机推荐
- Eddy's digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...
- SQL 防止注入
var strsql = "insert into Staff_Answer (ExamTitleID,QuestionsID,MultipleChoice,RightOption,Answ ...
- opensips编译安装时可能遇到的问题
错误一: ERROR: could not load the script in /usr/local//lib64/opensips/opensipsctl/opensipsdbctl.pgsql ...
- java web中get请求中文乱码在filter中解决
之前已经讲过get或者post方法的中文乱码问题,之前都是在每个方法中编写设置编码.如果程序变大,就会很繁琐,使用filter可以避免这种繁琐. 1)写一个encodingFilter进行编码设置 p ...
- Ubuntu 16.04下搭建kubernetes集群环境
简介 目前Kubernetes为Ubuntu提供的kube-up脚本,不支持15.10以及16.04这两个使用systemd作为init系统的版本. 这里详细介绍一下如何以非Docker方式在Ubun ...
- QT实现右键快捷菜单
[转自]:http://blog.csdn.net/rolland1989/article/details/5754575 QWidget及其子类都可有右键菜单,因为QWidget有以下两个与右键菜单 ...
- jquery获取元素索引值index()
jquery获取元素索引值index()方法实例. jquery获取元素索引值index()方法: jquery的index()方法 搜索匹配的元素,并返回相应元素的索引值,从0开始计数. 如果不给 ...
- MySQL错误代码大全(史上最全)
用任何主机语言调用MySQL时可能出现的错误.首先,列出了服务器错误消息.其次列出了客户端程序消息. B.1. 服务器错误代码和消息 服务器错误信息来自下述源文件: · 错误消息信息列在share/ ...
- python登陆Tom邮箱的代码一例
本文出处参考:http://www.cnblogs.com/LinuxHunter/archive/2010/11/30/1891635.html 在很多的python 教程中都会讲到登录邮箱或发送邮 ...
- Windows Phone 性能优化(二)
这篇文章的 demo 是在 (一)的基础上进行的调整,逻辑基本相似.本文只列和 上一篇出不同的代码. 为了实现自定义的虚拟化,把上一篇文章的 ListBox 换成 ScrollViewer + Ite ...