最长公共子序列(加强版) Hdu 1503 Advanced Fruits

时间:2023-03-09 03:27:30
最长公共子序列(加强版) Hdu 1503 Advanced Fruits

Advanced Fruits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1426    Accepted Submission(s):
719
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
这道题也是关于最长公共子序列的问题,但是,需要将公共子序列标记一下。刚开始做的时候想到是要求最长公共子序列,但是不知道怎么有效地将结果输出来,后来想想,只能将两个数组从后往前扫描一遍,将公共子序列单独处理,保存到另一个数组c中,最后将数组c倒序输出。
 /*
例如 apple peach
p e a c h
0 0 0 0 0 0
a 0 0 0 1 1 1
p 0 1 1 1 1 1
p 0 1 1 1 1 1
l 0 1 1 1 1 1
e 0 1 2 2 2 2
*/
#include<iostream>
#include <cstring>
using namespace std;
#define MAX 105
char ch1[MAX],ch2[MAX],ch[MAX];
int dp[MAX][MAX];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int i,j,k;
int m,n;
while(cin.getline(ch1,MAX,' ') && cin.getline(ch2,MAX,'\n'))
{
m = strlen(ch1);
n = strlen(ch2);
for(i=;i<=n;i++)
dp[][i] = ;
for(j=;j<=m;j++)
dp[j][] = ;
for(i=;i<=m;i++)
for(j=;j<=n;j++)
if(ch1[i-] == ch2[j-])
dp[i][j] = dp[i-][j-] + ;
else
dp[i][j] = max(dp[i-][j],dp[i][j-]);
//以上是求最长公共子序列
i = strlen(ch1),j = strlen(ch2);
k=;
while(i!= || j!=) //将所求的序列保存在数组ch中,从后往前依次比较两个数组
{
if(i==) //说明数组ch2还有剩余元素
{
ch[k++] = ch2[j-];
j--;
continue;
}
else if(j==) //说明数组ch1还有剩余元素
{
ch[k++] = ch1[i-];
i--;
continue;
}
else if(ch1[i-] != ch2[j-])
{
if(dp[i][j] == dp[i][j-])
{
ch[k++] = ch2[j-];
j--;
continue;
}
else if(dp[i][j] == dp[i-][j])
{
ch[k++] = ch1[i-];
i--;
continue;
}
}
else
{
ch[k++] = ch1[i-];
i--;j--;
continue;
}
}
for (i=k-;i>=;i--)
cout<<ch[i];
cout<<endl;
memset(ch1,,sizeof(ch1));
memset(ch2,,sizeof(ch2));
}
return ;
}