HDOJ--1503 Advanced Fruits
来源:互联网 发布:方正小标宋简体 mac 编辑:程序博客网 时间:2024/05/21 12:40
Advanced Fruits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2025 Accepted Submission(s): 1035
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
题目大意:额,这道题的大概意思是两个单词拼接成同一个单词,拼接的方式就是把共有的最长子序列抵消掉,然后输出就行了。
详见代码。
ac代码:
#include<stdio.h>#include<string.h>#define max(a,b) a>b?a:bint dp[1010][1010];struct node{int x,y;char c;}rp[1010];int main(){char a[1010],b[1010];while(scanf("%s %s",&a,&b)!=EOF){ //这个循环也是基本的模板,就是DP求最长子序列。 int i,j,k;int len1=strlen(a);int len2=strlen(b);memset(dp,0,sizeof(dp));for(i=1,k=-1;i<=len1;i++)for(j=1;j<=len2;j++){if(a[i-1]==b[j-1]){dp[i][j]=dp[i-1][j-1]+1;}elsedp[i][j]=max(dp[i-1][j],dp[i][j-1]);}if(dp[len1][len2]==0)printf("%s%s",a,b);else{i=len1,j=len2,k=-1;while(i!=0&&j!=0){//这个while循环用来记录子序列在两个原始序列中的位置。 if((dp[i][j]==dp[i-1][j-1]+1)&&a[i-1]==b[j-1]){rp[++k].x=i-1;rp[k].y=j-1;rp[k].c=a[i-1];i--;j--;}else if(dp[i-1][j]>dp[i][j-1])i--;elsej--;}int key=k;i=j=0;for(k=key;k>=0;k--){//这一部分是输出,分三部分输出,第一部分 不是子序列但是在最后一个子序列前面的字符。 while(i!=rp[k].x){printf("%c",a[i]);//第二部分,就是子序列这一部分的字符。 i++;}while(j!=rp[k].y){printf("%c",b[j]);j++;}printf("%c",rp[k].c);i++;j++;}printf("%s%s\n",a+rp[0].x+1,b+rp[0].y+1);//第三部分就是子序列之后的字符。 }}return 0;}
0 0
- HDOJ--1503 Advanced Fruits
- HDOJ-1503-Advanced Fruits
- HDOJ-1503 Advanced Fruits
- HDOJ 1503 Advanced Fruits
- HDOJ 1503 Advanced Fruits(LCS)
- hdoj-1503-Advanced Fruits【LCS】
- HDOJ 1503 Advanced Fruits(LCS+记录路径)
- hdoj 1503 Advanced Fruits (LCS 变形合并)
- HDOJ 题目1503 Advanced Fruits(LCS 递归,模板)
- hdoj 1503 Advanced Fruits 【最长公共子序列 变形】
- HDOJ 1503 Advanced Fruits (最长公共子序列 LCS)
- 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
- poj 1260 Pearls dp
- Advanced Fruits(HDU1503)LCS
- swap清内存
- acm 输入输出外挂
- 19个必须知道的Visual Studio快捷键
- HDOJ--1503 Advanced Fruits
- 【HDU 4760】Good Firewall(Trie水题)
- Hibernate学习——之延迟加载
- IO基础入门之I/O多路复用技术
- 计算机网络--TCP协议中的三次握手和四次挥手(图解)
- 算法——贝叶斯
- BZOJ 2752 [HAOI2012]高速公路(road) 线段树
- UVA 1225 Digit Counting
- [c语言]冒泡排序-------对折判断法