杭电 1503 Advanced Fruits
Description
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
Input is terminated by end of file.
Output
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的更多相关文章
- 最长公共子序列(加强版) 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(动态规划 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) ...
- Advanced Fruits HDU杭电1503【LCS的保存】
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
- hdu 1503 Advanced Fruits
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 思路:这是一道最长公共子序列的题目,当然还需要记录路径.把两个字符串的最长公共字串记录下来,在递 ...
- hdu 1503 Advanced Fruits(LCS输出路径)
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
- HDU 1503 Advanced Fruits(LCS+记录路径)
http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是 ...
- 题解报告:hdu 1503 Advanced Fruits(LCS加强版)
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
随机推荐
- POJ-325Corn Fields
链接:https://vjudge.net/problem/POJ-3254#author=freeloop 题意: 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ ...
- struts2的@Result annotation 如何添加params,并且在页面取值
http://www.bubuko.com/infodetail-2492575.html .............................................. 标签:lai ...
- 15 使用lambdas和闭包
1 使用lambdas和闭包 1.1 定义闭包 闭包是一个代码块,代替了方法或类. groovy中的闭包基于后边的构造方式:{list of parameters-> closur ...
- 092 Reverse Linked List II 反转链表 II
反转从位置 m 到 n 的链表.用一次遍历在原地完成反转.例如:给定 1->2->3->4->5->NULL, m = 2 和 n = 4,返回 1->4-> ...
- 1047 - Best couple 好题~
http://www.ifrog.cc/acm/problem/1047 思路很简单,跑一发floyd,然后再用km. 但是问题来了,这个有可能n != m.那怎么办? 其实可以补上一些不存在的点.来 ...
- php __autoload函数 加载类文件
面向对象的开发时,大家肯定都会遇到这样的问题,就是加载文件,一般都是加文件的头部inclue_once,require一大堆,看着很让人烦.当然你可以自己写程序来加载.php5以后引入了__autol ...
- MVC系列学习(十六)-区域的学习
1.查找控制器的过程 1.1调用其他项目中的控制器 a.先到网站根目录下的bin文件夹下,遍历所有的程序集 b.找到以Controller结尾的类 c.再找出其中继承了Controller的类 d.接 ...
- VC运行时库(/MD、/MT等)
VC项目属性→配置属性→C/C++→代码生成→运行时库 可以采用的方式有:多线程(/MT).多线程调试(/MTd).多线程DLL(/MD).多线程调试DLL(/MDd).单线程(/ML).单线程调试( ...
- HDU 3377 Plan (插头DP,变形)
题意:有一个n*m的矩阵,每个格子中有一个值(可能负值),要从左上角走到右下角,求路径的最大花费. 思路: 除了起点和终点外,其他的点可以走,也可以不走. (2)我用的是括号表示法,所以起始状态为') ...
- MATLAB批量修改图片名称
申明:转载请注明出处. 设在“D:\UserDesktop\pic\”目录下有很多张格式为jpg照片,命名不规则,如图. 现在用MATLAB批量修改所有图片的命名格式,改为1.jpg,2.jpg,.. ...