ACM-DP之Advanced Fruits——HDU1503

Advanced Fruits

Problem Description

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的题目,求最长公共子序列的一个延伸,题意大概为两个字符串合成一个串,要求公共子序列字符只出现一次,
这道题有一个special judge 红字,意思就是答案不唯一,比如 两个字符串peach 和 pear 你可以组合成:peachr也可以组合成
pearch,只要公共子序列出现一次即可。
我的方法是从后往前读,在根据优先顺序,先让前面输出。建立一个数组,存储读的方向,我的方向是
1 2
3 4,即1为向左上方读取(就是当碰到公共子序列的时候),2为向上读取(碰到 上面的数比左面的数大的时候),3为向左读取
(左面的数比上面的数大的时候),数大小关系,就是在求最长公共子序列长度中 建立的那个数组中的数大小的关系。
******最长公共子序列长度 不会求可以看我博客中另外一篇文章 ACM之Common Subsequence********
当读到2时,需要向上读,这时候要输出前面一个串的相应值,具体可看程序:

#include <iostream>
#include <string>
using namespace std;

int arr[1001][1001],brr[1001][1001];
char str1[1001],str2[1001];

// 输出,先执行函数sh再输出
void sh(int len1,int len2)
{
	if(len1==0 && len2==0)	return;

	if(brr[len1][len2]==2)		//当需要向上读
	{
		sh(len1-1,len2);			
		cout<<str1[len1-1];
	}
	else if(brr[len1][len2]==3)		//当需要向左读
	{
		sh(len1,len2-1);
		cout<<str2[len2-1];
	}
	else					// 需要向左上方读
	{
		sh(len1-1,len2-1);
		cout<<str1[len1-1];
	}
}

int main()
{
	int i,j,s1,s2,l;	
	
	while(cin>>str1>>str2)
	{
		s1=strlen(str1);
		s2=strlen(str2);

		// 初始化		
		memset(arr,0,sizeof(arr));  
		memset(brr,0,sizeof(brr));
		for(i=0;i<=s1;++i)	brr[i][0]=2;
		for(i=0;i<=s2;++i)	brr[0][i]=3;
		
		
		for(i=1;i<=s1;++i)
			for(j=1;j<=s2;++j)
			{
				if(str1[i-1]==str2[j-1])	{arr[i][j]=arr[i-1][j-1]+1;brr[i][j]=1;}
				else if(arr[i][j-1]>arr[i-1][j])	{arr[i][j]=arr[i][j-1];brr[i][j]=3;}
				else	{arr[i][j]=arr[i-1][j];brr[i][j]=2;}
			}

		sh(s1,s2);
		cout<<endl;
	}
	return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值