Advanced Fruits(好题,LCS的模拟)
Advanced Fruits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2052 Accepted Submission(s): 1053
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<stdio.h>
#include<string.h>
#define MAX(x,y) x>y?x:y
const int MAXN=;
int dp[MAXN][MAXN];
char s1[MAXN],s2[MAXN],ans[MAXN*];
int t1,t2,t;
void LCS(){
memset(dp,,sizeof(dp));
t1=strlen(s1+);t2=strlen(s2+);
for(int i=;i<=t1;i++){
for(int j=;j<=t2;j++){
if(s1[i]==s2[j])dp[i][j]=dp[i-][j-]+;
else{
dp[i][j]=MAX(dp[i-][j],dp[i][j-]);
}
}
}
}
void add(char a){
ans[t++]=a;
ans[t]='\0';
}
void print(){
while(dp[t1][t2]){
if(s1[t1]==s2[t2]){
add(s1[t1]);
t1--;t2--;
}
else{
if(dp[t1-][t2]>dp[t1][t2-]){
add(s1[t1]);
t1--;
}
else{
add(s2[t2]);
t2--;
}
}
}
while(t1>)add(s1[t1--]);
while(t2>)add(s2[t2--]);
}
int main(){
while(~scanf("%s%s",s1+,s2+)){
t=;
LCS();
print();
for(int i=t-;i>=;i--)printf("%c",ans[i]);
puts("");
}
return ;
}
Advanced Fruits(好题,LCS的模拟)的更多相关文章
- Advanced Fruits(HDU 1503 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 ...
- 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 ...
- LCS(打印全路径) POJ 2264 Advanced Fruits
题目传送门 题意:两个字符串结合起来,公共的字符只输出一次 分析:LCS,记录每个字符的路径 代码: /* LCS(记录路径)模板题: 用递归打印路径:) */ #include <cstdio ...
随机推荐
- 在 Inno Setup 中实现倒数N秒后激活按钮
原文 http://restools.hanzify.org/article.asp?id=67 timectrl.dll 为一个 6.5 KB 的按钮激活时间控制插件. 引用来自 Example1 ...
- 单元测试(UT)、功能测试(FT)(转)
纯个人总结: 单元测试(UT).功能测试(FT): 目的:1.尽量避免写的代码测试人员频繁的来找你其他地方又出问题了:2.提供的接口不可用:3.一个bug修复了引入了其他的bug或者其他用例变红了: ...
- 我(webabcd)的文章索引
[最后更新:2014.08.28] 重新想象 Windows Store Apps 系列文章 重新想象 Windows 8 Store Apps 系列文章 重新想象 Windows 8 Store A ...
- KMP快速模式匹配的java实现
假期实在无聊赖啊.把这个算法实现了一下即算是打发时间也算练练手了. KMP算法的关键是用归纳法计算失败函数.网上很详细了.下面直接给出代码. /** * * @author Vincent * */ ...
- offset
document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.docume ...
- Spring-----代码中使用注入的Properties配置属性
转载自:http://blog.csdn.net/hekewangzi/article/details/49990799
- JSP(二)
JSTL JSTL不仅可以实现EL所不能实现的逻辑循环或者条件判断,还有强大的定制标记. 使用JSTL 需要将两个文件("jstl.jar"和"standard.jar& ...
- (转)对mysql explain讲的比较清楚的
转自:http://www.blogjava.net/persister/archive/2008/10/27/236813.html 在 explain的帮助下,您就知道什么时候该给表添加索引,以使 ...
- slice,substr,substring
var s = '0123456789'; //slice 遇负数,则此负数值等同于 s.length + (负数) s.slice(3,6); //"345", 从位置3开始 ...
- Android 内部启动其他应用,以及打开指定qq聊天界面
在自己应用中打开第三方应用,有好多种方法,这里举例一种: //以打开微信为例,前提需要知道打开应用的包名,一般一个发布版本的应用,包名不会轻易改变的,但是,打开QQ就要注意了,毕竟QQ的发布版本有不下 ...
