Advanced Fruits HDU杭电1503【LCS的保存】
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 is terminated by end of file.
apple peach
ananas banana
pear peach
appleach
bananas
pearch
/*
这道题是这样输出的。首先读取s1的字符,读取时推断是否是公共字符。假设是就把s1前面的字符所有输出,
然后就来推断s2。(和推断s1是一样的),然后把公共字符输出,一直这样推断,直到最后一个公共字符
当最后一个公共字符推断完了后,把剩下的输出,先输s1,再s2. */ #include<stdio.h>
#include<string.h>
#define max(a,b) (a)>(b)? (a):(b)
char s1[200],s2[200];
int dp[200][200];
struct subsequence
{
int i,j;
char ch;
}common[120];
int main()
{
int i,j,k;
int len1,len2;
while(~scanf("%s%s",s1,s2))
{
len1=strlen(s1);
len2=strlen(s2);
memset(dp,0,sizeof(dp));
for(i=1;i<=len1;++i) //LCS
{
for(j=1;j<=len2;++j)
{
if(s1[i-1]==s2[j-1]) dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
if(dp[len1][len2]==0)//假设没有公共的,直接输出
{
printf("%s%s\n",s1,s2);
continue;
}
else//有就開始记录 。倒着记录
{
i=len1;j=len2;
k=0;
while(i>=1&&j>=1)
{
if(dp[i][j]==dp[i-1][j-1]+1&&s1[i-1]==s2[j-1])//最后一位同样,就存起来
{
common[k].i=i-1;//记录s1串的公共子序列(最后一个。倒数第二个.....) 元素的位置
common[k].j=j-1;//记录s2串的公共子序列(最后一个,倒数第二个.....) 元素的位置
common[k].ch=s1[i-1];//记录该公共字符
i--,j--;
k++;
}
else if(dp[i-1][j]>dp[i][j-1])//当去掉s1的最后一个元素的s1比去掉s2最后一个元素的最大公共子序列还要大的时候,说明s1的末尾不是最长子序列的一部分
i--;
else
j--;
}
}
i=j=0;
for(k=k-1;k>=0;--k)
{
while(common[k].i!=i)//先输出s1
{
printf("%c",s1[i]);
++i;
}
while(common[k].j!=j)//再输s2
{
printf("%c",s2[j]);
++j;
}
printf("%c",common[k].ch);
++i,++j;
}
while(s1[i]!='\0')//输出剩下的
{
printf("%c",s1[i]);
++i;
}
while(s2[j]!='\0')
{
printf("%c",s2[j]);
++j;
}
puts("");
} return 0;
}
Advanced Fruits HDU杭电1503【LCS的保存】的更多相关文章
- 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)
今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...
- 一个人的旅行 HDU杭电2066【dijkstra算法 || SPFA】
pid=2066">http://acm.hdu.edu.cn/showproblem.php? pid=2066 Problem Description 尽管草儿是个路痴(就是在杭电 ...
- 『ACM C++』HDU杭电OJ | 1418 - 抱歉 (拓扑学:多面体欧拉定理引申)
呕,大一下学期的第一周结束啦,一周过的挺快也挺多出乎意料的事情的~ 随之而来各种各样的任务也来了,嘛毕竟是大学嘛,有点上进心的人多多少少都会接到不少任务的,忙也正常啦~端正心态 开心面对就好啦~ 今天 ...
- 杭电 1503 Advanced Fruits
Description The company "21st Century Fruits" has specialized in creating new sorts of fru ...
- HDU Today HDU杭电2112【Dijkstra || SPFA】
http://acm.hdu.edu.cn/showproblem.php?pid=2112 Problem Description 经过锦囊相助,海东集团最终度过了危机,从此.HDU的发展就一直顺风 ...
- Choose the best route HDU杭电2680【dijkstra算法 || SPFA】
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...
- 畅通project续HDU杭电1874【dijkstra算法 || SPFA】
http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了非常多年的畅通project计划后.最终修建了非常多 ...
- find the safest road HDU杭电1596【Dijkstra || SPFA】
pid=1596">http://acm.hdu.edu.cn/showproblem.php?pid=1596 Problem Description XX星球有非常多城市,每一个城 ...
- 升级降级(期望DP)2019 Multi-University Training Contest 7 hdu杭电多校第7场(Kejin Player)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6656 题意: 有 1~n 个等级,你现在是1级,求升到n级的花费期望.会给你n个条件(i~i+1级升级 ...
随机推荐
- luogu2833 等式
题目大意 给出\(a,b,c,x_1,x_2,y_1,y_2\),求满足\(ax+by+c=0\),且\(x\in[x1,x2],y\in [y1,y2]\)的整数解有多少对. 题解 用扩展欧几里得算 ...
- Windows 10彻底关闭自动更新
关键点:把流量计费开启.
- CodeForces 131C C (组合)
There are n boys and m girls attending a theatre club. To set a play "The Big Bang Theory" ...
- 2018亚洲区预选赛北京赛站网络赛 D.80 Days 尺取
题面 题意:你带着K元要去n个城市,这n个城市是环形的,你可以选择任意一个起点,然后顺时针走,对于每个城市,到达时可以获得a元,但是从这里离开又需要花费b元,问你能否找到一个起点(输出花钱最少的那个) ...
- jTemplates的教程,包括{#if}{#foreach}{#for}的简单使用
最近在做一些局部刷新的分页工作,一般不使用既成的插件的话,就是在脚本里面重新渲染一个局部的html,把需要局部分页的模块重写一遍,还需要在控制器里再定义一个方法返回所需的局部数据,这种做法相当冗余,所 ...
- layui新手使用
1,首先最重要的是引入官方的layui.js layui.css文件 2,在自己的项目中新建一个目录 再在该目录下建一个js文件,js中写入 layui.define(['layer', 'form ...
- WordPress浏览次数统计插件:WP-Postviews使用
WP-Postviews使用 1.要让你的博客在页面上显示浏览次数,你需要修改你博客当前使用的主题,在主循环中插入以下代码: 1 <?php if(function_exists('the_vi ...
- 8) 十分钟学会android--Activity的生命周期之停止与重启
恰当的停止与重启我们的activity是很重要的,在activity生命周期中,他们能确保用户感知到程序的存在并不会丢失他们的进度.在下面一些关键的场景中会涉及到停止与重启: 用户打开最近使用app的 ...
- VM虚拟机NAT模式主机与虚拟机ping不通解决方案
VM虚拟机与真机通信三种模式, 桥接模式,NAT 模式 ,HOST-ONLY 模式. NAT模式 使用虚拟机的一个虚拟网卡做NAT网关,在nat网关上配dhcp ,或者直接用静态地址.就相当于形成了一 ...
- Sql Server 连接池
在看缓存的一篇文章中,看到连接池的三个字,来充充电. 原文连接 这篇讲了关键字 其实我们一直在使用SqlServer的连接池.在连接字符串中,Pooling为是否启用连接池,默认值为true,表示启用 ...