POJ 2264 Advanced Fruits--最长公共子序列

 

题目来源:POJ2264 Advanced Fruits

Description

Thecompany "21st Century Fruits" has specialized in creating new sortsof 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 newfruit emerges that tastes like a mixture between both of them. 

A big topic of discussion inside the company is "How should the newcreations be called?" A mixture between an apple and a pear could becalled an apple-pear, of course, but this doesn't sound very interesting. Theboss finally decides to use the shortest string that contains both names of theoriginal fruits as sub-strings as the new name. For instance, "applear"contains "apple" and "pear" (APPLEar and apPlEAR), andthere 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 acombination of two given fruits. Your algorithm should be efficient, otherwiseit is unlikely that it will execute in the alloted time for long fruitnames. 

Input

Each lineof the input contains two strings that represent the names of the fruits thatshould be combined. All names have a maximum length of 100 and only consist ofalphabetic characters. 
Input is terminated by end of file.

Output

For eachtest case, output the shortest name of the resulting fruit on one line. If morethan one shortest name is possible, any one is acceptable.

SampleInput

apple peach

ananas banana

pear peach

SampleOutput

appleach

bananas

pearch

 

考察点:LCS最长公共子序列路径记录


题目大意:给定两个长度不超过100的字符串A,B。从中找出一个最短的字符串C, 使得A,B都是C的子序列(不一定是子串)。


题目解析:我们很容易看出这道题目需要求出A,B串的最长公共子序列,因为要求得到的新字符串包含A,B且最短。但同时题目要求A,B都是C的子序列,所以在输出的时候需要注意不能改变A,B串的原顺序。


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s1[105],s2[105];//要输入的字符串s1,s2 
int str1,str2;//s1,s2字符串的长度 
int dp[105][105],flag[105][105];//dp数组用于更新LCS,flag数组记录最长公共子序列 
int a[105],b[105];//记录最长公共子序列在s1,s2中的位置 
void init()//初始化 
{
	str1=strlen(s1+1);
	str2=strlen(s2+1);
	memset(dp,0,sizeof(dp));
}
void LCS()//求解LCS 
{
	for(int i=1;i<=str1;i++)
	{
		for(int j=1;j<=str2;j++)
		{
			if(s1[i]==s2[j])
			{
				dp[i][j]=dp[i-1][j-1]+1;
				flag[i][j]=1;//来自左上 
			} 	
			else
			{
				if(dp[i][j-1]>=dp[i-1][j])
				{
					dp[i][j]=dp[i][j-1];
					flag[i][j]=2;//来自左 
				}
				else
				{
					dp[i][j]=dp[i-1][j];
					flag[i][j]=3;//来自上 
				} 
			}
		}
	}
}
void LCS_print()//输出结果 
{
	int i=str1,j=str2,k=0,h1,h2;
	while(i>0&&j>0)//反向记录LCS在s1,s2中出现的位置 
	{
		if(flag[i][j]==1)
		{
			a[k]=i;
			b[k++]=j;
			i--,j--;
		}
		else if(flag[i][j]==2)
			j--;
		else
			i--;
	}
	j=0,h1=1,h2=1;
	//先后输出s1,s2在每个最长公共子序列字符之前的字符,随后输出最长公共子序列中的一个字符 
	for(i=k-1;i>=0;i--)
	{
		for(;h1<a[i];h1++)
			putchar(s1[h1]);
		for(;h2<b[i];h2++)
			putchar(s2[h2]);
		h1++,h2++;//跳过最长公共子序列字符 
		putchar(s1[a[i]]);//输出最长公共子序列中的一个字符
	}
	//输出最后一个最长公共子序列字符之后的字符 
	for(int i=h1;i<=str1;i++)
		putchar(s1[i]);
	for(int i=h2;i<=str2;i++)
		putchar(s2[i]);
	putchar('\n');
} 
int main()
{
	while(~scanf("%s %s",s1+1,s2+1))
	{
		init();
		LCS();
		LCS_print();
	}
	return 0;
} 



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值