HDU 1503 Advanced Fruits

来源:互联网 发布:下载scratch软件 编辑:程序博客网 时间:2024/05/21 10:15

Advanced Fruits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2369    Accepted Submission(s): 1210
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 peachananas bananapear peach
 

Sample Output
appleachbananaspearch
 

Source
University of Ulm Local Contest 1999
 
大体题意:
给你两个单词,写出一个新的单词,使得新单词既包括左边的单词 又包括右边的单词!

看了看学长的博客,正好练习下最长公共子序列!

思路:
首先是正常的dp  如果发现dp[n][m] == 0则不存在最长公共子序列!
则直接输出s,t;
否则  建立一个结构体 包括i,j,char   i是s子序列的i位置,j是t子序列的一个位置!char ch 是当前的字符!
因为dp写的是dp[i] [j] = dp[i-1]....所以要倒着往回判断,所以向结构体保存答案时肯定倒着保存了!
最后先输出s的非公共序列  在输出t的非公共序列!在输出公共序列!在输出剩余的就行了!

#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn = 100 + 10;char s[maxn],t[maxn];int dp[maxn][maxn];struct Point{int i,j;char ch;}ans[maxn];bool LCS(int n,int m){memset(dp,0,sizeof(dp));for (int i = 1; i <= n; ++i)for (int j = 1; j <= m; ++j)if (s[i] == t[j]) dp[i][j] = dp[i-1][j-1] + 1;else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);if (dp[n][m] == 0)return false;int i = n ,j = m,cnt=0;while(i && j){if (dp[i][j] == dp[i-1][j-1] + 1 && s[i] == t[j]){//只能倒序保存答案! ans[cnt].i=i;ans[cnt].j=j;ans[cnt++].ch = s[i];i--;j--;}else if (dp[i-1][j] > dp[i][j-1]){--i;}else --j;}i = j = 1;for (int k = cnt-1; k >= 0; --k){//正序的ans! while(i != ans[k].i)printf("%c",s[i++]);while(j != ans[k].j)printf("%c",t[j++]);printf("%c",ans[k].ch);i++;j++;}printf("%s%s\n",s+1+ans[0].i,t+1+ans[0].j);return true;}int main(){while(scanf("%s%s",s+1,t+1) == 2){int len1 = strlen(s+1);int len2 = strlen(t+1);if (!LCS(len1,len2)){printf("%s%s\n",s+1,t+1);continue;}}return 0;}


0 0