刷题总结——advanced fruits(hud1503)
题目:
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.
InputEach 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.
OutputFor 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解决最长公共子序列问题····
关于这类问题参考:http://blog.csdn.net/yysdsyl/article/details/4226630/,这里就不多说了···
这道题其实和要求输出最长公共子序列问题的方法基本是一样的··只不过在dfs时除了要输出最长公共子序列以外还要按顺序输出两个单词其他部分的子串·····详细见dfs时的输出·····
看不懂可以结合上面链接中画的图
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<string>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
const int N=;
char s[N],t[N],ans[N];
int n,m,f[N][N],d[N][N];
inline void dp()
{
for(int i=;i<=n;i++) d[i][]=;
for(int i=;i<=m;i++) d[][i]=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(s[i]==t[j]) f[i][j]=f[i-][j-]+,d[i][j]=;
else if(f[i][j-]>f[i-][j]) f[i][j]=f[i][j-],d[i][j]=;
else f[i][j]=f[i-][j],d[i][j]=;
}
}
inline void dfs(int x,int y)
{
if(x==&&y==) return;
else
{
if(d[x][y]==) {dfs(x-,y-);printf("%c",s[x]);}
else if(d[x][y]==) {dfs(x,y-);printf("%c",t[y]);}
else if(d[x][y]==) {dfs(x-,y);printf("%c",s[x]);}
}
}
int main()
{
freopen("a.in","r",stdin);
while(~scanf("%s%s",s+,t+))
{
memset(f,,sizeof(f));memset(d,,sizeof(d));
n=strlen(s+);m=strlen(t+);
dp();dfs(n,m);printf("\n");
}
return ;
}
刷题总结——advanced fruits(hud1503)的更多相关文章
- Advanced Fruits(好题,LCS的模拟)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Advanced Fruits(HDU 1503 LCS变形)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- PTA刷题记录
考虑到PAT甲级考试和开学后的XCPC比赛,决定寒假把PAT (Advanced Level) Practice刷完,进度条会在这篇博客下更新.由于主要以记录为主,大体上不会像单篇题解那么详细,但是对 ...
- LeetCode刷题系列
LeetCode 我们工作面试和提高自身数据结构和算法能力的时候往往需要刷刷题,我选择LeetCode是通过一个留学论坛了解的.专业,覆盖语种全面. 提前说说刷题的心得: 尽量手写代码,少使用IDE的 ...
- ife任务刷题总结(一)-css reset与清除浮动
本文同时发布于本人的个人网站www.yaoxiaowen.com 百度创办的前端技术学院,是一个面向大学生的前端技术学习平台.虽然只有大学生才有资格报名,提交代码进行比赛排名.但是这并不妨碍我们这些初 ...
- 刷题ING...
我用codeVS刷题.. 努力准备!!
- XidianOJ 1020 ACMer去刷题吧
题目描述 刷题是每个ACMer必由之路,已知某oj上有n个题目,第i个题目小X能做对的概率为Pi(0<=Pi<=1,1<=i<=n) 求小X至少做对k道题的概率 输入 第一行输 ...
- 【BZOJ-4590】自动刷题机 二分 + 判定
4590: [Shoi2015]自动刷题机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 156 Solved: 63[Submit][Status ...
- NOI题库分治算法刷题记录
今天晚自习机房刷题,有一道题最终WA掉两组,极其不爽,晚上回家补完作业欣然搞定它,特意来写篇博文来记录下 (最想吐槽的是这个叫做分治的分类,里面的题目真的需要分治吗...) 先来说下分治法 分治法的设 ...
随机推荐
- angular设置反向代理
本地调试,需要用到服务器的api,发现chrome安全问题,需要解决跨域问题.现给出解决方案: 1.增加proxy.conf.json文件 位置与package.json文件同级(可指定) 2.pac ...
- 【转载】Alpha、Beta、RC、GA版本的区别
转自:http://www.blogjava.net/RomulusW/archive/2008/05/04/197985.html Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只 ...
- bootstrap 警告(Alerts)
本章将讲解警告(Alerts)以及bootstrap所提供的用于警告的class类.警告(Alerts)向用户提供了一种定义消息样式的方式.它们为典型的用户操作提供了上下文信息反馈. 您可以为警告框添 ...
- linux简单常用命令
除了yum命令,还有些简单的命令,在此记录一下,加深记忆: free -h 查询内存和交换分区. rpm -qa | grep libaio 查看当前环境是否安装某rpm软件包
- ReactiveCocoa概念解释进阶篇
1.ReactiveCocoa常见操作方法介绍 1.1 ReactiveCocoa操作须知 所有的信号(RACSignal)都可以进行操作处理,因为所有操作方法都定义在RACStream.h中,因此只 ...
- html页面简单访问限制
PS:突然发现博客园有密码保护功能,已经可以满足基本需求了.博客园还能备份自己的所有数据,做到了数据归用户所有,平台只是展示,真是良心网站,大赞. 想要通过一个站点放一些东西给一些人看,但是又不想让所 ...
- 使用jmeter做简单的压测(检查点、负载设置、聚合报告)
1.添加断言(检查点) 在需要压测的接口下添加--断言--响应断言,取接口响应中包含有的数据即可 检查点HTTP请求-->断言-->响应断言1.名称.注释2.Apply to//作用于哪里 ...
- 能力不足之 根据时序图转化为Verilog代码
不能够把时序图看的非常透彻,然后把时序图写成Verilog代码,有时候甚至搞不清楚信号之间的时序关系.
- json_encode() 避免转换中文
json_encode() 避免转换中文 我们都知道,json_encode()可以将数据转换为json格式,而且只针对utf8编码的数据有效,而且在转换中文的时候,将中文转换成不可读的”\u***” ...
- Thinkphp5 同时连接两个库
新建api/user.php <?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/8/25 * Time: 1 ...