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

Problem Description
The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn't work, but sometimes, in very rare cases, a new fruit emerges that tastes like a mixture between both of them.
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
Each line of the input contains two strings that represent the names of the fruits that should be combined. All names have a maximum length of 100 and only consist of alphabetic characters.

Input is terminated by end of file.

 
Output
For each test case, output the shortest name of the resulting fruit on one line. If more than one shortest name is possible, any one is acceptable.
 
Sample Input
apple peach ananas banana pear peach
 
Sample Output
appleach bananas pearch
 题解:
这个题就是让找出这两个串的最长公共子序列,然后加上这两个串中减去公共子序列的字符,输出就行;
我的思路就是先求出最长公共子序列的dp数组,然后再倒过来,模拟dp数组走的路径倒着记录就行了;
代码:

 #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的模拟)的更多相关文章

  1. Advanced Fruits(HDU 1503 LCS变形)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. HDU-1053:Advanced Fruits(LCS+路径保存)

    链接:HDU-1053:Advanced Fruits 题意:将两个字符串合成一个串,不改变原串的相对顺序,可将相同字母合成一个,求合成后最短的字符串. 题解:LCS有三种状态转移方式,将每个点的状态 ...

  3. hdu 1503:Advanced Fruits(动态规划 DP & 最长公共子序列(LCS)问题升级版)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. hdu 1503 Advanced Fruits(最长公共子序列)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. 最长公共子序列(加强版) Hdu 1503 Advanced Fruits

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. poj 2264 Advanced Fruits(DP)

    Advanced Fruits Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1944   Accepted: 967   ...

  7. hdu 1503 Advanced Fruits 最长公共子序列 *

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. 题解报告:hdu 1503 Advanced Fruits(LCS加强版)

    Problem Description The company "21st Century Fruits" has specialized in creating new sort ...

  9. LCS(打印全路径) POJ 2264 Advanced Fruits

    题目传送门 题意:两个字符串结合起来,公共的字符只输出一次 分析:LCS,记录每个字符的路径 代码: /* LCS(记录路径)模板题: 用递归打印路径:) */ #include <cstdio ...

随机推荐

  1. 工作无聊,闲来无事,自己学习 android入门

    工作无聊,闲来无事,自己学习. 最近几天看了看有关android的UI设计,布局以及android有关控件的知识,算是进一步了解了 android的相关内容. 明天就是周末了,明天及后天,我打算开始学 ...

  2. KeybMap 键盘映射工具更新至 V1.5(修订)

    KeybMap 更新至 V1.5,主要是增加了对一些多媒体键定义修改功能,也可以将任意一键定义为打开指定的程序. 3月9日略做修订. http://www.mympc.org/down/1/2005- ...

  3. System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...

  4. android 拍照 onCreate() 调用两次的问题

    拍照的代码网上都有就不写了!自己找下就ok了! 1 旋转屏幕导致问题! 这种情况很好解决:在androidManifest.xml 中设置activity 添加属性   android:configC ...

  5. C# 基础知识 (一).概念与思想篇

    在C#中有一些我自己认为比较独特的知识点,这些知识点是我经常使用的知识,但对它们的了解还是比较少的,所以通过查找资料学习,总结了这些独特的知识点并简单叙述,第一篇主要是一些概念和思想方面的知识.(后面 ...

  6. magent编译安装及常见错误

    安装magent到/usr/local/下 cd /usr/local mkdir magent cd magent/ wget http://memagent.googlecode.com/file ...

  7. 分享一个用安卓手机就能引导pc安装linux系统办法

    1.首先安卓手机下载软件DriveDroid.apk http://pan.baidu.com/s/1qW4pbT6 2.下载linux镜像文件放手机存储卡存储,放到Download/images/以 ...

  8. 使用HTML5/CSS3制作便签贴

    利用HTML5/CSS3,仅用5步就可以制作便签贴效果的HTML页面,效果图如下: (注:图里的文字纯属杜撰,搞笑目的,如有雷同,纯属巧合,谢谢!) 注:该效果可以在Safari, Chrome,Fi ...

  9. RadioButtonList选择事件onclick

    <asp:RadioButtonList ID="rbtnFInvoiceType" runat="server" onclick="check ...

  10. http头部信息研究

    1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type. 2. Accept-Charset: 浏览器申明自 ...