HDU1503:Advanced Fruits(LCS)
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.
ananas banana
pear peach
bananas
pearch
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
char str1[],str2[];
int dp[][],mark[][];
int len1,len2; void LCS()
{
memset(dp,,sizeof(dp));
for(int i=;i<=len1;i++)
mark[i][]=;
for(int i=;i<=len2;i++)
mark[][i]=-;
for(int i=;i<=len1;i++)
for(int j=;j<=len2;j++)
{
if(str1[i-]==str2[j-])
{
dp[i][j]=dp[i-][j-]+;
mark[i][j]=;
}
else if(dp[i-][j]>dp[i][j-])
{
dp[i][j]=dp[i-][j];
mark[i][j]=;
}
else
{
dp[i][j]=dp[i][j-];
mark[i][j]=-;
}
}
} void printfLCS(int i,int j)
{
if(i==&&j==)
return ;
if(mark[i][j]==)
{
printfLCS(i-,j-);
cout<<str1[i-];
}
else if(mark[i][j]==)
{
printfLCS(i-,j);
cout<<str1[i-];
}
else
{
printfLCS(i,j-);
cout<<str2[j-];
}
}
int main()
{
while(cin>>str1>>str2)
{
len1=strlen(str1);
len2=strlen(str2);
LCS();
printfLCS(len1,len2);
cout<<endl;
}
return ;
}
HDU1503:Advanced Fruits(LCS)的更多相关文章
- hdu 1503 Advanced Fruits(LCS输出路径)
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
- HDU 1503 Advanced Fruits (LCS,变形)
题意: 给两个水果名,要求他们的LCS部分只输出1次,其他照常输出,但是必须保持原来的顺序! 思路: 求LCS是常规的,但是输出麻烦了,要先求LCS,再标记两串中的所有LCS字符,在遇到LCS字符时, ...
- Advanced Fruits(HDU 1503 LCS变形)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Advanced Fruits(好题,LCS的模拟)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU-1053:Advanced Fruits(LCS+路径保存)
链接:HDU-1053:Advanced Fruits 题意:将两个字符串合成一个串,不改变原串的相对顺序,可将相同字母合成一个,求合成后最短的字符串. 题解:LCS有三种状态转移方式,将每个点的状态 ...
- 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(最长公共子序列)
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)T ...
- poj 2264 Advanced Fruits(DP)
Advanced Fruits Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1944 Accepted: 967 ...
随机推荐
- sql 查询表共多少列
1.oracle: select count(*) from user_tab_cols where table_name='表名';--表名含英文的话应为英文大写字母 2.mysql: select ...
- OpenGL编译问题随手记
1.error C2381: "exit" : 重定义:__declspec(noreturn) 不同 编译OpenGL Red Book 的例子时出现错误, stdl ...
- sharepoint:拥有完全控制权限的用户依然“拒绝访问”
//来源 http://www.cnblogs.com/jindahao/archive/2012/04/25/2468714.html 遇到问题: 拥有完全控制权限的用户依然拒绝访问. 可能的原因: ...
- ionic中$ionicPopover和$ionicModal
Popover可点多个按钮弹出同一个浮动框但内容不一样.那想要在同一页面弹出不同的浮动框怎么办呢? 这事就用到了$ionicModal,他和$ionicPopover一样的用法. 请看图: html: ...
- linux和windows双系统时间错误解决方法
转自http://www.2cto.com/os/201204/126212.html windows时间会慢8小时,原因: 两个概念: UTC即Universal Time Coordinated, ...
- javascript location对象
location用于获取或设置窗体的URL,并且可以用于解析URL. location.[属性|方法] 1.location对象属性图示: 2.location 对象属性: 3.location 对象 ...
- log4j2
转载自 Blog of 天外的星星: http://www.cnblogs.com/leo-lsw/p/log4j2tutorial.html Log4j 2的好处就不和大家说了,如果你搜了2,说明你 ...
- Major and minor numbers
The major nuber is the driver associated with the device, while the minor number is used by the kern ...
- 【第二篇】Volley的使用之加载图片
Volley加载图片有两种方式: 1,ImageRequest 来对网络图片进行请求,放入请求队列,获取后现在在控件上面. 2,NetworkImageView 最为自定义控件来自动加载网络图片. 3 ...
- Javascript获取不重复的随机数值
/** * 获取不重复随机数 * @param integer start 随机数最小值 * @param integer end 随机数最大值 * @param integer size 随机数获取 ...