题目:

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. centos7 python3 Saltstack配置

    Python安装完毕后,提示找不到ssl模块 pip is configured with locations that require TLS/SSL, however the ssl module ...

  2. BCB:UTF8Encode、AnsiToUtf8

    UTF8Encode: Call Utf8Encode to convert a Unicode string to UTF-8. WS is the Unicode string to conver ...

  3. php进行文件的强制下载

    浏览器下载文件,例如在浏览器中可以直接打开的文件(.gif /.txt等).在进行文件下载操作时,默认是通过浏览器直接打开,而不是下载保存文件.并且通过这种方法下载文件可以不暴漏下载文件所在的路径,可 ...

  4. LeetCode939

    问题:最小面积矩形 给定在 xy 平面上的一组点,确定由这些点组成的矩形的最小面积,其中矩形的边平行于 x 轴和 y 轴. 如果没有任何矩形,就返回 0. 示例 1: 输入:[[1,1],[1,3], ...

  5. vue组件:canvas实现图片涂鸦功能

    方案背景 需求 需要对图片进行标注,导出图片. 需要标注N多图片最后同时保存. 需要根据多边形区域数据(区域.颜色.名称)标注. 对应方案 用canvas实现涂鸦.圆形.矩形的绘制,最终生成图片bas ...

  6. day24 02 单继承(派生)

    day24 02 单继承(派生) 1.首先来看一个简单的例子 比如: 狗类的属性有:吃,喝,看门 鸟类的属性有:吃,喝,下蛋 看门和下蛋就是这两种动物不同的属性,而吃喝是两个共同的属性 以下代码实现了 ...

  7. drf版本控制 django缓存

    drf的版本控制 内置的版本控制类 from rest_framework.versioning import QueryParameterVersioning,AcceptHeaderVersion ...

  8. drf分页器

    drf分页器 1.第一种分页: 类似于django中的分页 2.第二种分页: 偏移分页 3.第三种分页: 加密分页(查询速度快) 无法跳跃 基本参数 from rest_framework.pagin ...

  9. w3resource_MySQL练习: Aggregate_functions

    w3resource_MySQL练习题:Aggregate_functions   1. Write a query to list the number of jobs available in t ...

  10. HDU 5025 Saving Tang Monk(状态转移, 广搜)

    #include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN], snake[maxN][maxN]; ]; int ...