hdu1503——LCS路径输出

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503

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

题目翻译

"21世纪水果"公司通过将一种水果的基因转移到另一种水果的基因组中,专门创造新型水果。大多数情况下,这种方法不起作用,但有时,在极少数情况下,会出现一种新水果,味道就像两者之间的混合物。‎
‎公司内部讨论的一大话题是"新作品应该如何‎
‎调用?当然,苹果和梨的混合物可以称为苹果梨,但这听起来并不很有趣。老板最终决定使用包含原始水果两个名称的最短字符串作为子字符串作为新名称。例如,"苹果"包含"苹果"和"梨"(APPLEar 和 apPlEAR),并且没有具有相同属性的较短字符串。‎
‎因此,蔓越莓和男孩浆果的组合被称为"男孩蔓越莓"或"蔓越莓"。‎
‎你的工作是编写一个程序,计算两个给定结果的组合的最短‎
‎名称。您的算法应该很有效,否则它不太可能在表示长果名的时间执行。‎

‎输入‎

‎输入的每一行包含两个字符串,表示应组合的水果的名称。所有名称的最大长度为 100,并且仅包含字母字符。‎
‎输入在文件末尾终止。‎

‎输出‎

‎对于每个测试用例,在一行上输出结果水果的最短名称。如果可能有多个最短名称,则任何一个都是可以接受的。

 

题意很好理解,就是先求出最长公共子序列并输出,再把不是最长公共子序列的输出就行了。变相的路径输出。

#include <iostream>
#include<cstring>
using namespace std;
string str1,str2;
int dp[1005][1005],visited1[1005],visited2[1005];
int path[1005][1005];
void printpath(int x,int y){
	if(!x&&!y) return ;
	if(path[x][y]==0) return ;
	if(path[x][y]==1){
		printpath(x-1,y-1);
//		if(str1[x]!=' ')
		cout<<str1[x-1];
//		visited1[x-1]=1;
//		visited2[y-1]=1;
	}
	else if(path[x][y]==2){
		printpath(x-1,y);
		cout<<str1[x-1];
	}
	else{
		printpath(x,y-1);
		cout<<str2[y-1];
	} 
}
int main(int argc, char** argv) {
	while(cin>>str1>>str2){
		int n=str1.size();
		int m=str2.size();
		memset(path,0,sizeof(path));
//		memset(visited1,0,sizeof(visited1));
//		memset(visited2,0,sizeof(visited2));
		for(int i = 0;i<=n;++i) path[i][0]=2;
		for(int i = 0;i<=m;++i) path[0][i]=3;
		for(int i = 1;i<=n;++i){
			for(int j = 1;j<=m;++j){
				if(str1[i-1]==str2[j-1]){
					dp[i][j]=dp[i-1][j-1]+1;
					path[i][j]=1;
				} 
				else{
					dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
					if(dp[i-1][j]>dp[i][j-1])
						path[i][j]=2;
					else path[i][j]=3;
				} 
			}
		}
//		printf("%d\n",dp[n][m]);
		printpath(n,m);
//		for(int i = 0;i<n;++i)
//			if(!visited1[i])
//				cout<<str1[i];
//		for(int i = 0;i<m;++i)
//		    if(!visited2[i])
//				cout<<str2[i];
		cout<<endl;
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值