hdoj.1503 Advanced Fruits【最长公共子序列】 2015/04/15
来源:互联网 发布:网络上的怼是什么意思 编辑:程序博客网 时间:2024/05/29 08:31
Advanced Fruits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1750 Accepted Submission(s): 902
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
#include<stdio.h>#include<string.h>#define MAXN 110char s1[MAXN],s2[MAXN],s3[MAXN*2];int arr[MAXN][MAXN],dp[MAXN][MAXN];int i,j,k,l1,l2,m,n,flag;int p,q;void Init(){ // 最长公共子序列 memset(arr,0,sizeof(arr)); l1 = strlen(s1); l2 = strlen(s2); for( i = 0 ; i <= l1 ; ++i ) dp[i][0] = 3; for( j = 0 ; j <= l2 ; ++j ) dp[0][j] = 1; for( i = 1 ; i <= l1 ; i++ ){ for( j = 1 ; j <= l2 ; j++ ){ if( s1[i-1] == s2[j-1] ){ arr[i][j] = arr[i-1][j-1] + 1; dp[i][j] = 2; } else if( arr[i][j-1] >= arr[i-1][j] ){ arr[i][j] = arr[i][j-1]; dp[i][j] = 1; } else { arr[i][j] = arr[i-1][j]; dp[i][j] = 3; } } }}void solve(int x,int y){ //递归输出 if( x == 0 && y == 0 ) return ; switch(dp[x][y]){ case 1:solve(x,y-1); printf("%c",s2[y-1]); break; case 2:solve(x-1,y-1); printf("%c",s1[x-1]); break; case 3:solve(x-1,y); printf("%c",s1[x-1]); break; }}int main(){ while(scanf("%s %s",s1,s2)!=EOF){ Init(); solve(l1,l2); printf("\n"); } return 0;}
0 0
- hdoj.1503 Advanced Fruits【最长公共子序列】 2015/04/15
- hdoj 1503 Advanced Fruits 【最长公共子序列 变形】
- HDOJ 1503 Advanced Fruits (最长公共子序列 LCS)
- 暑期dp46道(29) HDOJ 1503 Advanced Fruits 最长公共子序列
- Advanced Fruits(最长公共子序列)
- hdu 1503 Advanced Fruits(最长公共子序列变形)
- HDU 1503(Advanced Fruits)最长公共子序列+路径记录
- hdu 1503 Advanced Fruits 最长公共子序列变形
- HDU 1503 Advanced Fruits (LCS最长公共子序列)
- hdu 1503 Advanced Fruits 公共子序列
- POJ 2264 Advanced Fruits(最长公共子序列)
- POJ 2264 Advanced Fruits--最长公共子序列
- hdu1503 Advanced Fruits(DP 最长公共子序列变形)
- hdu 1503 Advanced Fruits(最长公共子序列的应用)
- hdu 1503 Advanced Fruits(最长公共子序列+路径记录)
- HDU 1503 Advanced Fruits(还原最长公共子序列、记录路径)
- hdu 1503 Advanced Fruits (公共子序列 的输出)
- HDU 1503 Advanced Fruits (公共子序列+输出)
- C文件读写操作
- java进程间通信
- 最长回文子串算法入门经典
- 回文数
- PHP中的超全局变量
- hdoj.1503 Advanced Fruits【最长公共子序列】 2015/04/15
- How should one read a book?
- 限定$scope的范围
- php中sql语句汇总
- 第四章 基于对象的编程风格(什么是this指针)
- zoj1383Binary Numbers
- NS2导入Eclipse过程
- CART Decision Tree and two pruning theory
- 网络编程UDP的一些基本操作应用