首页 > 代码库 > HDU 1503 Advanced Fruits
HDU 1503 Advanced Fruits
Advanced Fruits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3622 Accepted Submission(s): 1872
Special JudgeProblem DescriptionThe 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.
InputEach 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.
OutputFor 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 Inputapple peachananas bananapear peachSample Outputappleachbananaspearch
题目大意就是找到最长公共子序列。然后输出输入的两个字符串,注意,取出来的公共子序列只输出一遍!
好吧,我承认我当时也看不懂题意。只能按照公共子序列的套路来咯
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 int dp[105][105]; 5 using namespace std; 6 struct Node{//i,j记录最长公共子序列在a,b串的位置,ch保存字符 7 int i,j; 8 char ch; 9 }; 10 int main() 11 { 12 char a[105],b[105]; 13 while(~scanf("%s%s",a+1,b+1)) 14 { 15 memset(dp,0,sizeof(dp)); 16 int alen=strlen(a+1); 17 int blen=strlen(b+1); 18 for(int i=1;i<=alen;i++) 19 { 20 for(int j=1;j<=blen;j++) 21 { 22 if(a[i]==b[j]){ 23 dp[i][j]=dp[i-1][j-1]+1; 24 }else{ 25 dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 26 } 27 } 28 } 29 Node ans[105]; 30 int len=0; 31 if(dp[alen][blen]==0) 32 printf("%s%s",a,b); 33 else{ 34 int i=alen,j=blen; 35 while(i!=0&&j!=0)//倒序取出最长公共子序列,得到最长公共子序列的dp过程的逆推 36 { 37 if(dp[i][j]==dp[i-1][j-1]+1&&a[i]==b[j]){ 38 ans[len].i=i; 39 ans[len].j=j; 40 ans[len++].ch=a[i]; 41 i--; 42 j--; 43 }else if(dp[i-1][j]>dp[i][j-1]){ 44 i--; 45 }else{ 46 j--; 47 } 48 } 49 } 50 int i=1,j=1; 51 for(int k=len-1;k>=0;k--)//输出结果 52 { 53 while(i!=ans[k].i){ 54 cout<<a[i]; 55 i++; 56 } 57 while(j!=ans[k].j){ 58 cout<<b[j]; 59 j++; 60 } 61 cout<<ans[k].ch; 62 i++; 63 j++; 64 } 65 printf("%s%s",a+1+ans[0].i,b+1+ans[0].j); 66 cout<<endl; 67 } 68 return 0; 69 }
HDU 1503 Advanced Fruits
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。