Advanced Fruits (最大公共子序列的路径打印)

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.

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 Input
apple peach
ananas banana
pear peach
Sample Output
appleach
bananas
pearch


题目意思:这道题我一开始一直没有看懂样例,不知道这道题是怎么来组合最大公共子序列和其他的字符的,但是同学的一句话让我豁然开朗,所给的A串和B串都可以看成要求的新串的子序列,或者可以说
要求一个最小公共母序列。

解题思路:根据LCS的原理,我们需要考虑最长公共子序列的每一个字符的来源和求解过程,回溯整个求解过程,对照DP表,我们就能窥见端倪,只要打印最长公共子序列的来源路径即可。

表中灰色格格所代表的字符组成的字符串,或许就可以称之为最短公共母序列了





递归方法打印路径,


 1 #include<cstdio>
 2 #include<cstring>
 3 char a[110],b[110];
 4 int dp[110][110],lena,lenb,mark[110][110];
 5 void LCS()
 6 {
 7     int i,j;
 8     memset(dp,0,sizeof(dp));
 9     for(i=0;i<lena;++i)
10        mark[i][0]=1;///x轴
11     for(i=0;i<lenb;++i)
12        mark[0][i]=-1;///y轴
13     for(i=1;i<=lena;++i)
14     {
15         for(j=1;j<=lenb;++j)
16         {
17             if(a[i-1]==b[j-1])
18             {
19                 dp[i][j]=dp[i-1][j-1]+1;
20                 mark[i][j]=0;
21             }
22             else if(dp[i-1][j]>=dp[i][j-1])
23             {
24                 dp[i][j]=dp[i-1][j];
25                 mark[i][j]=1;
26             }
27             else
28             {
29                 dp[i][j]=dp[i][j-1];
30                 mark[i][j]=-1;
31             }
32         }
33     }
34 }
35  
36 void out(int x,int y)
37 {
38     if(!x&&!y)
39        return ;
40     else if(mark[x][y]==0)
41     {
42         out(x-1,y-1);
43         printf("%c",a[x-1]);
44     }
45     else if(mark[x][y]==1)
46     {
47         out(x-1,y);
48         printf("%c",a[x-1]);
49     }
50     else if(mark[x][y]==-1)
51     {
52         out(x,y-1);
53         printf("%c",b[y-1]);
54     }
55 }
56  
57 int main()
58 {
59     while(scanf("%s%s",&a,&b)!=EOF)
60     {
61         lena=strlen(a);
62         lenb=strlen(b);
63         LCS();
64         out(lena,lenb);
65         printf("\n");///注意换行 
66     }
67     return 0;
68 }

 

非递归方法,回溯,这个代码主要是我对照着DP表的模拟得到的
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stack>
 4 #include<algorithm>
 5 using namespace std;
 6 #define N 210
 7 int dp[N][N];
 8 char c;
 9 int main()
10 {
11     char a[N];
12     char b[N];
13     int la,lb;
14     int i,j;
15     while(scanf("%s%s",a,b)!=EOF)
16     {
17         memset(dp,0,sizeof(dp));
18         la=strlen(a);
19         lb=strlen(b);
20         for(i=1; i<=la; i++)
21         {
22             for(j=1; j<=lb; j++)
23             {
24                 if(a[i-1]==b[j-1])
25                 {
26                     dp[i][j]=dp[i-1][j-1]+1;
27                 }
28                 else
29                 {
30                     dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
31                 }
32             }
33         }
34         i=la;
35         j=lb;
36         stack<char>s;
37         while(dp[i][j])
38         {
39             if(i==0||j==0)
40             {
41                 break;
42             }
43             if(dp[i][j]==dp[i-1][j])
44             {
45                 i--;
46                 s.push(a[i]);
47             }
48             else if(dp[i][j]==dp[i][j-1])
49             {
50                 j--;
51                 s.push(b[j]);
52             }
53             else if(dp[i][j]>dp[i-1][j-1])
54             {
55                 i--;
56                 j--;
57                 s.push(a[i]);
58             }
59         }
60 
61         while(i!=0)///剩下的字符
62         {
63             i--;
64             s.push(a[i]);
65         }
66         while(j!=0)
67         {
68             j--;
69             s.push(b[j]);
70         }
71         while(!s.empty())
72         {
73             c=s.top();
74             printf("%c",c);
75             s.pop();
76         }
77         printf("\n");
78     }
79     return 0;
80 }

 

 
 

posted @ 2018-07-24 22:01  王陸  阅读(686)  评论(0编辑  收藏  举报