您的位置:首页 > 产品设计 > UI/UE

【DP|LCS+输出路径】HDU-1503 Advanced Fruits

2014-12-03 17:38 330 查看


Advanced Fruits


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


Special Judge

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

 

题意:给你两个字符串,将这两个字符串作为子串合并成一个最短的字符串。

思路:显然要使得合并后的字符串最短,就要使得用共用部分最多,也就是说求得一个最长公共子序列,它就是共用部分。LCS问题的变形。但是要输出路径。这也难倒我了,唉。

当路径处在一个数组上的时候,我知道用fa[ ]数组记录就行了,但是现在路径是交错在两个数组上的,这个时候用数组mark[i][j]来记录第i和第j个位置是由哪一个数组走过来的。之后就递归输出解即可。

注意,需要正确的初始化mark数组才不至于越界。

代码如下:

/**
* ID: j.sure.1
* PROG:
* LANG: C++
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <climits>
#include <iostream>
#define For(i, x, y) for(int i=x; i<y; i++)
#define For_(i, x, y) for(int i=x; i>=y; i--)
#define Mem(f, x) memset(f, x, sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Pri(x) printf("%d\n", x)
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 111;
char s[2]
;
int n1, n2, dp

, mark

;

void PR(int i, int j)
{
if(!i && !j) return ;
switch(mark[i][j])
{
case 0: PR(i-1, j-1);
printf("%c", s[0][i-1]);
break;
case 1: PR(i-1, j);
printf("%c", s[0][i-1]);
break;
case 2: PR(i, j-1);
printf("%c", s[1][j-1]);
break;
}
}//有可能s0和s1当中有一个已经输出完了,这个时候就看到初始化的必要了

int main()
{
#ifdef J_Sure
freopen("000.in", "r", stdin);
//freopen("999.out", "w", stdout);
#endif
while(~scanf("%s%s", s[0], s[1])) {
n1 = strlen(s[0]);
n2 = strlen(s[1]);
Mem(dp, 0);
Mem(mark, 0);
For(i, 1, n1+1) {
mark[i][0] = 1;
}//s1输出完了,剩余的就是输出s0的字符
For(j, 1, n2+1) {
mark[0][j] = 2;
}//s2输出完了,剩余的就是输出s1的字符
For(i, 1, n1+1) {
For(j, 1, n2+1) {
if(s[0][i-1] == s[1][j-1]) {
dp[i][j] = dp[i-1][j-1] + 1;
}
else {
if(dp[i-1][j] > dp[i][j-1]) {
dp[i][j] = dp[i-1][j];
mark[i][j] = 1;//(i, j)处填的是s0
}
else {
dp[i][j] = dp[i][j-1];
mark[i][j] = 2;
}
}
}
}
PR(n1, n2);
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: