动态规划12最长公共子序列

Advanced Fruits

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 33   Accepted Submission(s) : 13
Special Judge

Font: Times New Roman | Verdana | Georgia

Font Size:  

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
AC代码

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

#define MAX 103
int via[MAX];//记录第一个字符串中最长字串的下标例如如果str="bacdegf",最长字串是"aef",则将a,e,f在str中的下标存放在这里面
int f[MAX][MAX];
int way[MAX][MAX];//记录f[i][j]是从什么方式得来的,可能是str[i]==str[j],f[i][j]=f[i-1][j-1]+1,或者f[i][j]=f[i-1][j],或者f[i][j]=f[i][j-1]

void dfs(int p, int q)
{
	if (p == 0 || q == 0)
		return;
	if (way[p][q] == 0)//一种方式
	{
		via[p] = 1;
		dfs(p - 1, q - 1);
	}
	else if (way[p][q] == 1)//另一种方式
		dfs(p - 1, q);
	else if (way[p][q] = 2)//最后一种方式
		dfs(p, q - 1);

}

int main()
{
	string str1;
	string str2;
	while (cin >> str1 >> str2)
	{
		str1 = " " + str1;//稍微处理一下,使下标不用+-1
		str2 = " " + str2;
		memset(f, 0, sizeof(f));
		memset(via, 0, sizeof(via));
		memset(way, -1, sizeof(way));
		for (int i = 1; i < str1.size(); ++i)
		{
			for (int j = 1; j < str2.size(); ++j)
			{
				if (str1[i] == str2[j])
				{
					f[i][j] = f[i - 1][j - 1] + 1;//LIS的状态转移方程
					way[i][j] = 0;//这种方式的way[i][j]定义为0,与后面的dfs相对应
				}
				else
				{
					if (f[i - 1][j] > f[i][j - 1])
					{
						way[i][j] = 1;//这种方式的way[i][j]定义为1,与后面的dfs相对应
						f[i][j] = f[i - 1][j];
					}
					else
					{
						way[i][j] = 2;//这种方式的way[i][j]定义为2,与后面的dfs相对应
						f[i][j] = f[i][j - 1];
					}
				}
			}
		}
		dfs(str1.size()-1, str2.size()-1);
		int st = 1;
		for (int i = 1; i < str1.size(); ++i)//神奇并完美的输出方式
		{
			if (via[i] == 1)
			{
				for (int j = st; j < str2.size(); ++j)
				{
					if (str1[i] == str2[j])
					{
						st = j + 1;
						break;
					}
					cout << str2[j];
				}
			}
			cout << str1[i];
		}
		for (int i = st; i < str2.size(); ++i)
			cout << str2[i];
		cout << endl;
	}
	system("pause");
	return 0;
}




猜你喜欢

转载自blog.csdn.net/qq_36921652/article/details/79187409