HDU 1503.Advanced Fruits

215

题目



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


题解



很明显,需要用到>最长公共子序列<的内容
在输出字符串时,除了最长公共子序列的字符,还要输出其经过的字符
结合最长公共子序列的图

代码


/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
using namespace std;

const int maxn = 205;
char a[maxn],b[maxn],c[maxn];

//最长公共子序列
//输入字符串a 及其长度 字符串b 及其长度 保存最长公共子序列的数组
//字符从0开始
int LCS(char *a,char *b,char s[] = NULL) {
int len1 = strlen(a);
int len2 = strlen(b);
char *aa = a - 1;
char *bb = b - 1;
//声明二维数组
int * m = new int[(len1 + 1)*(len2 + 1)];
int **dp = new int *[len1 + 1];
for(int i = 0;i <= len1;i++)
dp[i] = m + i*(len2 + 1);
//初始化
for(int i = 0;i <= len1;i++)
dp[i][0] = 0;
for(int i = 0;i <= len2;i++)
dp[0][i] = 0;
//动态规划
for(int i = 1;i <= len1;i++)
for(int j = 1;j <= len2;j++)
if(aa[i] == bb[j])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j],dp[i][j - 1]);
//如果c未传值
if(s == NULL)
return dp[len1][len2];
//逆序推出一条符合串
int ans = dp[len1][len2];
int x = len1;
int y = len2;
int it = ans;
int pos = len1 + len2 - it;
s[pos] = '\0';

while(x || y) {
if(x - 1 >= 0 && dp[x - 1][y] == it) {
s[--pos] = aa[x--];
continue;
}
if(y - 1 >= 0 && dp[x][y - 1] == it) {
s[--pos] = bb[y--];
continue;
}
s[--pos] = aa[x];
--it;

x--;
y--;
}
return ans;
}

bool Do() {
if(scanf("%s%s",a,b) == EOF)
return false;
LCS(a,b,c);
printf("%s\n",c);
return true;
}

int main() {
while(Do());
return 0;
}
发布评论
  • 点击查看/关闭被识别为广告的评论