1503 Advanced Fruits
来源:互联网 发布:nc瑞士军刀打开端口 编辑:程序博客网 时间:2024/06/05 16:53
链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1503
Advanced Fruits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 880 Accepted Submission(s): 410
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.
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.
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 peachananas bananapear peach
Sample Output
appleachbananaspearch
Source
University of Ulm Local Contest 1999
Recommend
linle
具体看代码:
#include<iostream>#include<cstring>using namespace std;int L[102][102]; /*存储a[i]与b[j]的最长子序列的L[i][j]*/int s[102][102]; /*搜索过程中的子序列状态*/int aa[101];int bb[101]; /* 如果bb[i] = 1 代表b[i]字符是公共的,0 则不是*/int main(){char a[101],b[101]; int i,j,k;for(i=0;i<102;i++) /*初始化*/{L[i][0] = 0;L[0][i] = 0;}while(scanf("%s%s",a+1,b+1)!=EOF){memset(aa,0,sizeof(aa)); /*初始化*/memset(bb,0,sizeof(bb));int la = strlen(a+1);int lb = strlen(b+1);for(i=1;i<=la;i++)for(j=1;j<=lb;j++){if(a[i] == b[j]){L[i][j] = L[i-1][j-1] + 1;s[i][j] = 1;}else if(L[i-1][j] >= L[i][j-1]){L[i][j] = L[i-1][j];s[i][j] = 2;}else{L[i][j] = L[i][j-1];s[i][j] = 3;}}k = L[la][lb];i = la;j = lb;/*搜索最长公共子序列字符*/while(i&&j){switch(s[i][j]){case 1: aa[i] = bb[j] = 1; /*标记*/k--;j--;i--;break;case 2: i-- ;break;case 3: j-- ;break;}}/*构造新型品种的名字(含前两种名字的信息)*//*采用插空,即按原来的顺序将不属于LCS的字符给插进来*/for(k=1,i=1,j=1;k<=L[la][lb];k++){while(aa[i] == 0)cout<<a[i++];while(bb[j]==0)cout<<b[j++];cout<<a[i++];j++;}for(;i<=la;i++)cout<<a[i];for(;j<=lb;j++)cout<<b[j];cout<<endl;}return 0;}
- HDU 1503 Advanced Fruits
- HDU 1503 Advanced Fruits
- HDU 1503 - Advanced Fruits
- hdu 1503 Advanced Fruits
- 1503 Advanced Fruits
- hdu 1503 Advanced Fruits
- hdu 1503 Advanced Fruits
- hdu 1503 advanced fruits
- HDU 1503 Advanced Fruits
- hdu 1503 Advanced Fruits
- HDOJ--1503 Advanced Fruits
- hdu 1503 Advanced Fruits
- HDOJ-1503-Advanced Fruits
- hdu 1503 Advanced fruits
- HDOJ-1503 Advanced Fruits
- HDU 1503 Advanced Fruits
- hdu 1503 Advanced Fruits
- HDOJ 1503 Advanced Fruits
- svn: Working copy xxx locked
- framebuffer
- Postgresql 帐号密码修改方法
- Android日历部分资料整理
- 关于图形化编程软件sikuli 初学者使用时可能容易出现的一些问题
- 1503 Advanced Fruits
- dreamweaver + xampp + php + mysql 数据库连接发生http 405错误及“发生不知名错误”的解决办法
- ListView控件使用简介
- 创建子程序的理由
- 显示
- Kill a postgresql session/connection
- C# Listview控件使用实例
- 神舟电脑已走进死胡同 专家建议上市后迅速转型
- 用Java集合中的Collections.sort方法对list排序的两种方法