Advanced Fruits(LCS)

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 peach
ananas banana
pear peach

Sample Output

appleach
bananas
pearch

        本题是简单的最长公共子序列的模板题,采用动态规划的方法,定义一个二维数组dp[i][j],表示单词a的前i个字母和单词b的前j个字母的最长公共子序列,若a[i]=b[j],则dp[i][j]=dp[i-1][j-1]+1。若a[i]!=b[j],则dp[i][j]=max(dp[i-1][j],dp[i][j-1])。最后求出的dp[i-1][j-1]就是a和b的最长公共子序列的长度。

        而本题,除了要找到最长公共子序列,还要将余下的字母按顺序插入到子序列中,所以再定义一个方向数组,表示插入的顺序。0表示输出当前字母,即dp[i][j]取其左上角的值;-1表示输出dp[i][j]的取值是dp[i][j-1],即dp[i][j]取其上方的值;1表示dp[i][j]的取值是dp[i-1][j],即dp[i][j]取其左方的值。若a[i]=b[j],则输出二者其中之一;若不相等,若dp[i-1][j]>dp[i][j-1],则输出a[i-1],若dp[i][j-1]>dp[i-1][j],则输出b[j-1]。代码如下:

#include<iostream>
#include<string.h>
using namespace std;
char a[105],b[105];
int dp[105][105],flag[105][105];
void out(int x,int y)
{
	if(x==0 && y==0)
		return;
	if(flag[x][y]==0)
	{
		out(x-1,y-1);
		printf("%c",a[x-1]);
	}
	else if(flag[x][y]==-1)
	{
		out(x,y-1);
		printf("%c",b[y-1]);
	}
	else if(flag[x][y]==1)
	{
		out(x-1,y);
		printf("%c",a[x-1]);
	}
}
int main()
{
	while(cin>>a>>b)
	{
		int len_a=strlen(a);
		int len_b=strlen(b);
		for(int i=0;i<=len_a;i++)//若j为0时,dp[i][0]都应等于dp[i-1][0];
			flag[i][0]=1;
		for(int i=0;i<=len_b;i++)//若i为0时,dp[0][j]都应等于dp[0][j-1];
			flag[0][i]=-1;
		
		for(int i=1;i<=len_a;i++)
		{
			for(int j=1;j<=len_b;j++)
			{
				if(a[i-1]==b[j-1])
				{
					dp[i][j]=dp[i-1][j-1]+1;
					flag[i][j]=0;
				}
				else if(dp[i][j-1]>=dp[i-1][j])
				{
					dp[i][j]=dp[i][j-1];
					flag[i][j]=-1;
				}
				else if(dp[i-1][j]>=dp[i][j-1])
				{
					dp[i][j]=dp[i-1][j];
					flag[i][j]=1;
				}
			}
		}
		out(len_a,len_b);
		cout<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Q_M_X_D_D_/article/details/83592011