博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1503 Advanced Fruits 最长公共子序列 *
阅读量:6250 次
发布时间:2019-06-22

本文共 3420 字,大约阅读时间需要 11 分钟。

Advanced Fruits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3613    Accepted Submission(s): 1867 Special Judge

Problem Description
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. 
 

 

Input
Each 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.
 
 

 

Output
For 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
 

 

Source
 
 
题意:这道题就是给你两个单词,然后你要把两个单词拼接成一个新单词,使得新单词的子序列中包含两个单词,并且要使这个新单词最短。所以这道题就是求最长公共子序列. 思路:将每个字符都进行标记,看两个字符串中对应的字符究竟处于什么状态,然后输出,其标记为公共子串的字符只输出一次.
 
注意在所有的最长公共子序列中,数组都要从1开始,别从零开始,方便dp。
 
#include
#include
#include
#include
using namespace std; int dp[110][110]; char a[110],b[110]; char s[220]; int main() { while(~scanf("%s%s",a+1,b+1)) { memset(dp,0,sizeof(dp)); int n = strlen(a+1); int m = strlen(b+1); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ //求出最长公共子序列长度 if(a[i] == b[j]){ dp[i][j] = dp[i-1][j-1]+1; } else dp[i][j] = max(dp[i][j-1],dp[i-1][j]); } } int i,j,k; i = n; j = m; k = 0; while(dp[i][j]){
//将要输出的字符串先到存到s数组中 if(a[i] == b[j]){
//当a,b,中字符相等 s[k++] = a[i]; i--; j--; } else if(dp[i][j-1] < dp[i-1][j]){ //注意这种情况选取a数组还是b数组 s[k++] = a[i]; i--; } else if(dp[i][j-1] >= dp[i-1][j]){ s[k++] = b[j]; j--; } } while(i>=1){
//a中不存在公共子序列的部分 s[k++] = a[i]; i--; } while(j>=1){
//b中不存在公共子序列的部分 s[k++] = b[j]; j--; } for(int t = k-1;t>=0;t--){ cout << s[t]; } cout << endl; } return 0; }

 

 

转载于:https://www.cnblogs.com/l609929321/p/7251279.html

你可能感兴趣的文章
从Yii2的Request看其CSRF防范策略
查看>>
composer安装yii2或者laravel报错
查看>>
springmvc 环境配置图
查看>>
Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法
查看>>
Android学习网站
查看>>
[CareerCup] 13.7 Node Pointer 节点指针
查看>>
UML用例图中泛化、扩展、包括
查看>>
prism 4 模块配置 管理
查看>>
String
查看>>
News: Visual Studio Code support debugging Linux Apps
查看>>
【BZOJ】2956: 模积和
查看>>
【转载】COM 组件设计与应用(二)——GUID 和 接口
查看>>
struts2 标签问题----日期显示
查看>>
c++ http请求
查看>>
Android 读取蓝牙设备信息开发
查看>>
Build制作模型
查看>>
配置域主DNS服务器
查看>>
HTML的16个全局属性
查看>>
RMI,RPC,SOAP对比分析
查看>>
LeetCode - Regular Expression Matching
查看>>