博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最长公共子序列(加强版) Hdu 1503 Advanced Fruits
阅读量:5252 次
发布时间:2019-06-14

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

Advanced Fruits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1426    Accepted Submission(s): 719
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
 
 
这道题也是关于最长公共子序列的问题,但是,需要将公共子序列标记一下。刚开始做的时候想到是要求最长公共子序列,但是不知道怎么有效地将结果输出来,后来想想,只能将两个数组从后往前扫描一遍,将公共子序列单独处理,保存到另一个数组c中,最后将数组c倒序输出。
1 /* 2 例如 apple peach 3     p e a c h 4   0 0 0 0 0 0 5 a 0 0 0 1 1 1 6 p 0 1 1 1 1 1 7 p 0 1 1 1 1 1 8 l 0 1 1 1 1 1 9 e 0 1 2 2 2 210 */ 11 #include
12 #include
13 using namespace std;14 #define MAX 10515 char ch1[MAX],ch2[MAX],ch[MAX];16 int dp[MAX][MAX];17 int max(int a,int b)18 {19 return a>b?a:b;20 }21 int main()22 {23 int i,j,k;24 int m,n;25 while(cin.getline(ch1,MAX,' ') && cin.getline(ch2,MAX,'\n'))26 {27 m = strlen(ch1);28 n = strlen(ch2);29 for(i=0;i<=n;i++)30 dp[0][i] = 0;31 for(j=0;j<=m;j++)32 dp[j][0] = 0;33 for(i=1;i<=m;i++)34 for(j=1;j<=n;j++)35 if(ch1[i-1] == ch2[j-1])36 dp[i][j] = dp[i-1][j-1] + 1;37 else38 dp[i][j] = max(dp[i-1][j],dp[i][j-1]);39 //以上是求最长公共子序列40 i = strlen(ch1),j = strlen(ch2);41 k=0;42 while(i!=0 || j!=0) //将所求的序列保存在数组ch中,从后往前依次比较两个数组43 {44 if(i==0) //说明数组ch2还有剩余元素45 {46 ch[k++] = ch2[j-1];47 j--;48 continue;49 }50 else if(j==0) //说明数组ch1还有剩余元素51 {52 ch[k++] = ch1[i-1];53 i--;54 continue;55 }56 else if(ch1[i-1] != ch2[j-1])57 {58 if(dp[i][j] == dp[i][j-1])59 {60 ch[k++] = ch2[j-1];61 j--;62 continue;63 }64 else if(dp[i][j] == dp[i-1][j])65 {66 ch[k++] = ch1[i-1];67 i--;68 continue;69 }70 }71 else72 {73 ch[k++] = ch1[i-1];74 i--;j--;75 continue;76 }77 }78 for (i=k-1;i>=0;i--)79 cout<

 

转载于:https://www.cnblogs.com/yazhou/p/3643751.html

你可能感兴趣的文章
Js时间处理
查看>>
Java项目xml相关配置
查看>>
三维变换概述
查看>>
第三次作业
查看>>
vue route 跳转
查看>>
【雷电】源代码分析(二)-- 进入游戏攻击
查看>>
Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。...
查看>>
Linux中防火墙centos
查看>>
mysql新建用户,用户授权,删除用户,修改密码
查看>>
FancyCoverFlow
查看>>
JS博客
查看>>
如何设置映射网络驱动器的具体步骤和方法
查看>>
ASP.NET WebApi 基于OAuth2.0实现Token签名认证
查看>>
283. Move Zeroes把零放在最后面
查看>>
Visual Studio Code 打开.py代码报Linter pylint is not installed解决办法
查看>>
Python 数据类型
查看>>
S5PV210根文件系统的制作(一)
查看>>
centos下同时启动多个tomcat
查看>>
slab分配器
查看>>
数据清洗
查看>>