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 题目意思就是找到一个最短序列,使a,b字符串都是这个序列里的,且前后顺序不变(不一定连续) 代码我也是不太懂,就不解释了
 #include<cstdio>
#include<cstring>
int flag[][],dp[][],lena,lenb;
char a[],b[];
void out(int x,int y)
{
if(x==&&y==)
{
return ;
}
else
{
if(flag[x][y] == )
{
out(x-,y-);
printf("%c",a[x-]);
}
if(flag[x][y] == )
{
out(x-,y);
printf("%c",a[x-]);
}
if(flag[x][y] == -)
{
out(x,y-);
printf("%c",b[y-]);
}
}
}
int main()
{
while(scanf("%s%s",&a,&b)!=EOF)
{
lena=strlen(a);
lenb=strlen(b); int i,j;
memset(dp,,sizeof(dp));
for(i = ; i < lena ; i++)
flag[i][]=;
for(i = ; i < lenb ; i++)
flag[][i]=-;
for(i = ; i <= lena ; i++)
{
for(j = ; j <= lenb ; j++)
{
if(a[i-] == b[j-])
{
dp[i][j]=dp[i-][j-]+;
flag[i][j]=;
} else
{
if(dp[i-][j] >= dp[i][j-])
{
dp[i][j]=dp[i-][j];
flag[i][j]=;
}
else
{
dp[i][j]=dp[i][j-];
flag[i][j]=-;
}
}
}
}
out(lena,lenb);
printf("\n");
}
}

 

杭电 1503 Advanced Fruits的更多相关文章

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

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

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

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

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

    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) ...

  5. Advanced Fruits HDU杭电1503【LCS的保存】

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

  6. hdu 1503 Advanced Fruits

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 思路:这是一道最长公共子序列的题目,当然还需要记录路径.把两个字符串的最长公共字串记录下来,在递 ...

  7. hdu 1503 Advanced Fruits(LCS输出路径)

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

  8. HDU 1503 Advanced Fruits(LCS+记录路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是 ...

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

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

随机推荐

  1. [HDU1595] find the longest of the shortest

    题目链接: 点我 题意: 给定一个\(n\)个点,\(m\)条边的带权无向图,起点为\(1\),终点为\(n\),现在可以删去其中的一条边,求一种删边方案使得剩下图的最短路值最大,输出这个最短路的长度 ...

  2. HDU 5869 Different GCD Subarray Query 树状数组 + 一些数学背景

    http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...

  3. vue中的number

    今天在vue文档中看到有number这样一个修饰符 觉得挺方便的就尝试了一下下面是代码 <body> <div id="box"> <input ty ...

  4. hdu3487Play with Chain(splay)

    链接 简单的两种操作,一种删除某段区间,加在第I个点的后面,另一个是翻转区间.都是splay的简单操作. 悲剧一:pushdown时候忘记让lz=0 悲剧二:删除区间,加在某点之后的时候忘记修改其父亲 ...

  5. 难道这就是gin中间件的原理,一个装饰者模式而已?

    func wrapCtx(handler func(ctx *gin.Context)) gin.HandlerFunc { return func(c *gin.Context) { //获取请求的 ...

  6. # iOS Block的本质(三)

    iOS Block的本质(三) 上一篇文章iOS Block的本质(二)中已经介绍过block变量的捕获,本文继续探寻block的本质. 1. block对对象变量的捕获,ARC 环境 block一般 ...

  7. iOS Category实现原理

    iOS Category实现原理 实现原理 我们不主动引入 Category 的头文件,Category 中的方法都会被添加进主类中.我们可以通过 - performSelector: 等方式 对 C ...

  8. LR中下载文件的脚本

    #include "web_api.h" Action(){ int iflen; //文件大小 long lfbody; //响应数据内容大小 web_url("xxx ...

  9. ejb2.0用本地引用提高EJB访问效率

    用本地引用提高EJB访问效率 EJB 1.0和1.1规范只定义了一种在EJB组件中引用另一组件的方法,即通过Bean的远程接口.如果两个Bean都在同一个容器之内,则这种网络开销是不必要的.为解决这个 ...

  10. spring 上传附件

    jsp: <form class='uk-form' action="savelead" method="post" enctype="mult ...