hdu1503:advanced Fruits "LCS"

Source: Internet
Author: User

Advanced Fruits Time Limit:2000/1000ms (java/other) Memory limit:65536/32768k (Java/other) total submission (s): 5 Accepted Submission (s): 2Special Judge problem DescriptionThe Company "21st Century Fruits" have specialized in creating n EW 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 bet Ween both of them.
A big topic of discussion inside the "How should the new creations be called?" A mixture between an apple and a pear could is called an apple-pear, the course, but this doesn ' t sound very interesting. The boss finally decides 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 are no shorter string that have the SAM E property.

A combination of a cranberry and a boysenberry would therefore be called a "boysecranberry" or a "Craboysenberry", for Exa Mple.

Your job is-to-write a program, computes such a shortest name for a combination of of both given fruits. Your algorithm should be efficient, otherwise it's unlikely that it'll execute in the alloted time for long fruit names .

Inputeach line of the input contains, strings that represent the names of the fruits, is should. All names has a maximum length of between 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 was possible, any one is acceptable.
Sample Input
Apple Peachananas bananapear Peach

Sample Output
Appleachbananaspearch

This problem is mainly used in the LCS and backtracking methods ~ ~ ~ when backtracking in order to let them sequentially output ... Need to make a mark ~ ~ ~

Ac-code:

#include <cstdio> #include <cstring>using namespace std;int mark[105][105];char s1[105],s2[105];void Print (int a,int b) {if (a==0&&b==0) return, else if (a==0) {print (a,b-1);p rintf ("%c", S2[b-1]);} else if (b==0) {print (a-1,b);p rintf ("%c", S1[a-1]);} else if (mark[a][b]==1) {print (a-1,b-1);p rintf ("%c", S1[a-1]);} else if (mark[a][b]==2) {print (a-1,b);p rintf ("%c", S1[a-1]);} Else{print (a,b-1);p rintf ("%c", S2[b-1]);}} int main () {int Len1,len2,i,j;int dp[105][105];while (~scanf ("%s%s", &s1,&s2)) {memset (dp,0,sizeof (DP)); memset (Mark,0,sizeof (Mark)); Len1=strlen (S1); Len2=strlen (S2); for (i=1;i<=len1;i++) for (j=1;j<=len2;j++) {if ( S1[i-1]==s2[j-1]) {dp[i][j]=dp[i-1][j-1]+1;mark[i][j]=1;} else if (Dp[i-1][j]>dp[i][j-1]) {dp[i][j]=dp[i-1][j];mark[i][j]=2;} else{dp[i][j]=dp[i][j-1];mark[i][j]=3;}} Print (LEN1,LEN2);p rintf ("\ n");} return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1503:advanced Fruits "LCS"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.