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的listview组件

    http://www.cnblogs.com/menlsh/archive/2013/03/15/2962350.html http://www.tuicool.com/articles/IveeI3 ...

  2. 反调试技术(Delphi版)

    1.程序窗口句柄检测原理:用FindWindow函数查找具有相同窗口类名和标题的窗口,如果找到就说明有OD在运行//****************************************** ...

  3. C# 迪杰斯特拉(Dijkstra)算法

    Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. 其基本思想是,设置顶点集合S并不断地作 ...

  4. Android Studido下的应用性能优化总结--布局优化

    前言:一个应用的成功=产品设计*性能 ,再此我们不讨论一个应用的设计,那交给我们可爱又可恨的产品经理和UI设计师来决定!所以这里分步骤讨论如何提升一个应用的性能,这里先探讨布局优化问题. 布局优化 避 ...

  5. Android实现ListView或GridView首行/尾行距离屏幕边缘距离

    直接上关键属性: 设置ListView或GridView的android:clipToPadding = true, 然后通过paddingTop和paddingBottom设置距离就好了.

  6. 数组中出现次数超过一半的数字 -java

    数组中出现次数超过一半的数字 -java 方法一: 数组排序,然后中间值肯定是要查找的值. 排序最小的时间复杂度(快速排序)O(NlogN),加上遍历. 方法二: 使用散列表的方式,也就是统计每个数组 ...

  7. [置顶] Codeforces Round #190 (Div. 2)(完全)

    好久没有写博客了,一直找不到有意义的题可以写,这次也不算多么有意义,只是今天是比较空的一天,趁这个时候写一写. A. B. 有一点贪心,先把每个拿去3的倍数,余下0或1或2,然后三个一起拿. 对于以上 ...

  8. 【floyd存字典序路径】【HDU1385】【Minimum Transport Cost】

    题目大意 求多组i到j的最短路径 并输出字典序最小.... 现在只会floyd的方式 利用dis[i][j] 表示i到j的路径中i 后面的节点 更新是比较dis[i][j] dis[i][k]. 记住 ...

  9. Linux 08

    1按照视频里提出的几点要求完善使ls命令模仿windows下dir命令输出的脚本 {printf $6" "$7" \t";if (substr($1,1,1) ...

  10. mysql 重启

    /etc/init.d/mysql restart /etc/init.d/mysql stop /etc/init.d/mysql start