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级升级 ...
随机推荐
- 错误: su: 无法设置组: 不允许的操作
到 /bin目录下,用ls -l 看下su文件的权限是不是rwxr-xr-x或者-rwxrwxrwx 执行这条命令chmod ug+s su
- '无法将“vue”项识别为 cmdlet、函数、脚本文件或可运行程序的名称' 或 'vue不是内部或外部命令' 的解决方法
如果在使用 vue 初始化项目的时候提示: vue : 无法将“vue”项识别为 cmdlet.函数.脚本文件或可运行程序的名称.请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次. 或者: ...
- contest hunter 6803 导弹防御塔
没什么好写的.写写这题吧 拆点,把一个防御塔拆成m个,表示第i次攻击.瞎yy就好啊 #include<cstdio> #include<iostream> #include&l ...
- RBF网络——核心思想:把向量从低维m映射到高维P,低维线性不可分的情况到高维就线性可分了
RBF网络能够逼近任意的非线性函数,可以处理系统内的难以解析的规律性,具有良好的泛化能力,并有很快的学习收敛速度,已成功应用于非线性函数逼近.时间序列分析.数据分类.模式识别.信息处理.图像处理. ...
- nyoj--284--坦克大战(bfs模板)
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" i ...
- Python使用装饰器自动调用父类__init__
众所周知,Python中class的构造函数实际是__new__(),但是如果我们执行p1=Point()的时候,不仅会调用Point的__new__方法,而且会调用Point的__init__方法. ...
- 「Canvas」玩转
作者: 糖少 简介 Canvas是 HTML5 新增的,一个可以使用脚本(通常为JavaScript)在其中绘制图像的 HTML 元素.它可以用来制作照片集或者制作简单(也不是那么简单)的动画,甚至可 ...
- Qt5.9 WebEngine 概述
Qt WebEngine模块提供了一个web浏览器, 在不使用本地浏览器的情况下, 它可以很容易地把Web内容嵌入到Qt应用程序中. Qt WebEngine为渲染HTML, XHTML和SVG文档, ...
- 爬虫之Urllib库的基本使用
官方文档地址:https://docs.python.org/3/library/urllib.html 什么是Urllib Urllib是python内置的HTTP请求库包括以下模块urllib.r ...
- Python中断言与异常的区别
异常,在程序运行时出现非正常情况时会被抛出,比如常见的名称错误.键错误等. 异常: >>> s Traceback (most recent call last): File &qu ...