题目:

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.

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)的更多相关文章

  1. Advanced Fruits(好题,LCS的模拟)

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

  2. Advanced Fruits(HDU 1503 LCS变形)

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

  3. PTA刷题记录

    考虑到PAT甲级考试和开学后的XCPC比赛,决定寒假把PAT (Advanced Level) Practice刷完,进度条会在这篇博客下更新.由于主要以记录为主,大体上不会像单篇题解那么详细,但是对 ...

  4. LeetCode刷题系列

    LeetCode 我们工作面试和提高自身数据结构和算法能力的时候往往需要刷刷题,我选择LeetCode是通过一个留学论坛了解的.专业,覆盖语种全面. 提前说说刷题的心得: 尽量手写代码,少使用IDE的 ...

  5. ife任务刷题总结(一)-css reset与清除浮动

    本文同时发布于本人的个人网站www.yaoxiaowen.com 百度创办的前端技术学院,是一个面向大学生的前端技术学习平台.虽然只有大学生才有资格报名,提交代码进行比赛排名.但是这并不妨碍我们这些初 ...

  6. 刷题ING...

    我用codeVS刷题.. 努力准备!!

  7. XidianOJ 1020 ACMer去刷题吧

    题目描述 刷题是每个ACMer必由之路,已知某oj上有n个题目,第i个题目小X能做对的概率为Pi(0<=Pi<=1,1<=i<=n) 求小X至少做对k道题的概率 输入 第一行输 ...

  8. 【BZOJ-4590】自动刷题机 二分 + 判定

    4590: [Shoi2015]自动刷题机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 156  Solved: 63[Submit][Status ...

  9. NOI题库分治算法刷题记录

    今天晚自习机房刷题,有一道题最终WA掉两组,极其不爽,晚上回家补完作业欣然搞定它,特意来写篇博文来记录下 (最想吐槽的是这个叫做分治的分类,里面的题目真的需要分治吗...) 先来说下分治法 分治法的设 ...

随机推荐

  1. 使用Timer组件制作左右飘动的窗体

    实现效果: 知识运用: Form类的Left和Top属性 实现代码: private void timer1_Tick(object sender, EventArgs e) { Rectangle ...

  2. apropos linux

    Apropos adj. 恰当的,关于,就...而言 adv. 顺便地,恰当地 All my suggestions apropos the script were accepted. 我所有有关该剧 ...

  3. Vue中npm run build报“Error in parsing SVG: Unquoted attribute value”

    自己做的一个Vue项目,在打包时老是报这个错误 # Error in parsing SVG: Unquoted attribute value 查了查网上说的,都说报错原因是压缩和抽离CSS的插件中 ...

  4. javaweb基础(12)_session详解

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  5. 对O(logN)复杂度的推导

    之前一直对O(logN)这个复杂度如何推导出的存在疑问,这段时间看了一些算法相关的内容,正好看到这个问题,大略研究了一下算是基本解答了我的疑惑:现记录如下 假设有一棵高为H的满二叉树,则它的节点共有N ...

  6. Django项目SECRET_KEY等敏感信息保存

    在我们做完django项目后,向生产环境部署时,为了避免一些敏感信息被有心人利用,我们应该将其保护起来,比如说在settings配置中的一些密码等内容存在操作系统内,通过调用来使用,比如下面这种做法: ...

  7. C++:100阶乘数组输出

    #include <iostream> using namespace std; int main(){ int i =1; int a[2048]={0}; while(i !=101) ...

  8. Laravel核心解读--Console内核

    Console内核 上一篇文章我们介绍了Laravel的HTTP内核,详细概述了网络请求从进入应用到应用处理完请求返回HTTP响应整个生命周期中HTTP内核是如何调动Laravel各个核心组件来完成任 ...

  9. Linux远程传输命令scp

    指令:scp在不同的linux主机间复制文件带有Security的文件copy,基于ssh登录. 有些linux发行版没有自带scp,因此需要安装scp# yum -y install openssh ...

  10. gulp的安装和使用

    安装nodejs -> 全局安装gulp -> 项目安装gulp以及gulp插件 -> 配置gulpfile.js -> 运行任务 1.去nodejs官网安装nodejs 2. ...