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 ...
随机推荐
- javascript模式
http://developer.51cto.com/art/201212/372725.htm http://justjavac.com/javascript/2012/12/14/model-vi ...
- 键盘有没有NKRO ?微软帮你测
玩家甚至媒体的解读是错的,所以小编在此重点说明一些概念.并分享如何测试.在许多游戏与软体中都会使用组合键功能,也就是同时按下特定几个按键之后就能触发特别的功能,简单的说就是一些动作的快捷键.不过,有时 ...
- Mysql explain 查看分区表
mysql> explain select * from ClientActionTrack where startTime>'2016-08-25 00:00:00' and start ...
- Boost程序库完全开发指南——深入C++“准”标准库(第3版)
内容简介 · · · · · · Boost 是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库,有着“C++‘准’标准库”的美誉. Boost 由C++标准委员会部分成员所设立的Bo ...
- hdu 1050 Moving Tables_贪心
题意:你搬n个桌子,桌子从一个地方搬到另一个地方,走廊只允许同时一个桌子通过,教室分布在两边,奇数在一边,偶数在一边,当桌子不冲突时可以同时搬运,冲突时要等别的那个桌子搬完再搬. 思路:因为奇数桌子在 ...
- Linux dirname、basename 指令
http://blog.sina.com.cn/s/blog_9d074aae01013ctk.html 一.dirname指令 1.功能:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然 ...
- hdu 5620 KK's Steel(推理)
Problem Description Our lovely KK has a difficult mathematical problem:he has a N(1≤N≤1018) meters s ...
- 【线段树】【3-21个人赛】【同样的problemB】
同一道题 http://blog.csdn.net/zy691357966/article/details/44680121 区间查询最大值 用线段树 比单调队列慢 #include <cst ...
- LeetCode Day2
Power of Two /** * LeetCode: Power of Two * Given an integer, write a function to determine if it is ...
- < meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" />
目录(?)[-] 1 meta http-equiv X-UA-Compatible content chrome1 1 meta http-equiv X-UA-Compatible cont ...