题目:

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. mtDNA|ctDNA|cpDNA|

    5.9细胞器基因组是编码细胞器蛋白质的环状DNA分子 细胞器中除真核细胞线粒体DNA(mtDNA)是线性的外,都是环状分子,比如叶绿体DNA(ctDNA,cpDNA).因为单个细胞器有几套不同拷贝的细 ...

  2. [].indexOf.call()学习

    今天看到闭包一道题,就是一个li列表,点击列表控制台输出对应的索引.这里考察了var的作用域问题和闭包对外部变量的引用问题,有几种解决方法. html: <ul> <li>te ...

  3. tensorflow目标检测API安装及测试

    1.环境安装配置 1.1 安装tensorflow 安装tensorflow不再仔细说明,但是版本一定要是1.9 1.2 下载Tensorflow object detection API  下载地址 ...

  4. 洛谷 2387/BZOJ 3669 魔法森林

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 3765  Solved: 2402[Submit][Statu ...

  5. JQuery图片轮播实例

    HTML+CSS代码: <!doctype html> <html> <head> <meta charset="utf-8"> & ...

  6. 第6章 AOP与全局异常处理6.5-6.11 慕课网微信小程序开发学习笔记

    https://coding.imooc.com/learn/list/97.html 目录: 第6章 AOP与全局异常处理6-1 正确理解异常处理流程 13:236-2 固有的处理异常的思维模式与流 ...

  7. 使用python3调用MyQR库生成动态二维码(附源代码)

    可生成普通二维码.带图片的艺术二维码(黑白与彩色).动态二维码(黑白与彩色). GitHub:https://github.com/sylnsfar/qrcode 中文版:https://github ...

  8. LeetCode(162) Find Peak Element

    题目 A peak element is an element that is greater than its neighbors. Given an input array where num[i ...

  9. Cinder配置多Ceph后端步骤

    1. 检查cinder当前backend配置 使用cinder service-list,查看cinder-volume服务的Host字段格式. 旧版格式: 新版格式: 旧版中Host字段是cinde ...

  10. 如何用ADMINISTRATOR登陆WIN7

    Windows 7系统出于安全考虑,将系统超级管理员帐户(Administrator)隐藏了,不允许"普通用户"使用.很多时候特别是安装一些应用软件时,由于兼容的问题,普通权限的用 ...