HDU 1503 Advanced Fruits (公共子序列+輸出)
Advanced Fruits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3619 Accepted Submission(s): 1869
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.
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.
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
Source
找到最長公共子序列,在輸出兩個字串,要合併子序列。
#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
int dp[1200][1200];
int f1[1200],f2[1200];
int main()
{
char s1[1200];
char s2[1200];
while(~scanf("%s %s",s1,s2))
{
memset(dp,0,sizeof dp);
memset(f1,-1,sizeof f1);
memset(f2,-1,sizeof f2);
int t1=strlen(s1);
int t2=strlen(s2);
for(int i=0; i<t1; i++)
{
for(int j=0; j<t2; j++)
{
if(s1[i]==s2[j])
dp[i+1][j+1]=dp[i][j]+1;
else
dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
}
}
int i=t1,j=t2,k=0;
int pathi[1200],pathj[1200];
while(dp[i][j])
{
if(dp[i][j]==dp[i-1][j])
i--;
else if(dp[i][j]==dp[i][j-1])
j--;
else
{
pathj[k]=j-1;
pathi[k++]=i-1;
i--;
j--;
}
}
int p=0;
for(int i=k-1; i>=0; i--)
{
f1[pathi[i]]=++p;
f2[pathj[i]]=p;
}
/* for(int i=0;i<t1;i++)
{
printf("%d ",f1[i]);
}
printf("\n");
for(int i=0;i<t2;i++)
{
printf("%d ",f2[i]);
}
printf("\n");*/
int l1=0,l2=0;
p=1;
while(l1<t1&&l2<t2)
{
if(f1[l1]==-1)
{
printf("%c",s1[l1]);
l1++;
}
else if(f2[l2]==-1)
{
printf("%c",s2[l2]);
l2++;
}
else if(f1[l1]==p)
{
printf("%c",s1[l1]);
p++;
l1++;
l2++;
}
}
while(l1<t1)
{
printf("%c",s1[l1]);
l1++;
}
while(l2<t2)
{
printf("%c",s2[l2]);
l2++;
}
printf("\n");
}
return 0;
}
相關文章
- 最長公共子序列LCS 輸出所有LCS
- 最長公共子序列
- 最長公共子序列(JAVA)Java
- 力扣1143. 最長公共子序列 動態規劃之最長公共子序列力扣動態規劃
- 線性dp:最長公共子序列
- 最長公共子序列求方案數
- java 實現 最長公共子序列Java
- 最長公共子序列 Longest Common Subsequence
- LeetCode 1143.最長公共子序列LeetCode
- 最長迴文子序列(不連續) 可輸出迴文序列
- 動態規劃-最長公共子序列動態規劃
- 動態規劃——最長公共子序列動態規劃
- 最長公共子序列你學會了嗎
- 動態規劃(最長公共子序列LCS)動態規劃
- 動態規劃之最長公共子序列求解動態規劃
- 51Nod 1006 最長公共子序列Lcs
- [題解]P1439 【模板】最長公共子序列
- 最長公共子序列問題—動態規劃sdut動態規劃
- 字串篇(python)—兩個字串的最長公共子序列字串Python
- 51nod-基因匹配+luogu-【模板】最長公共子序列
- 動態規劃經典問題----最長公共子序列動態規劃
- 【LeetCode動態規劃#14】子序列系列題(最長遞增子序列、最長連續遞增序列、最長重複子陣列、最長公共子序列)LeetCode動態規劃陣列
- 經典演算法題每日演練——最長公共子序列演算法
- 詳解動態規劃最長公共子序列--JavaScript實現動態規劃JavaScript
- [譯] Swift 演算法學院 - 最長公共子序列演算法Swift演算法
- 領釦LintCode演算法問題答案-77. 最長公共子序列演算法
- [Java] 藍橋杯ADV-202 演算法提高 最長公共子序列Java演算法
- 檔案輸入和輸入出處理(六)-序列化和反序列化
- CF #683 (Div. 2)D. Catching Cheaters【最長公共子序列LCS變形】
- 以最長公共子序列問題理解動態規劃演算法(DP)動態規劃演算法
- 淺談最長公共子序列引發的經典動態規劃問題動態規劃
- 『無為則無心』Python序列 — 23、Python序列的公共APIPythonAPI
- lCS(最長公共子串)
- CF1503E 2-Coloring
- 輸入n個元素組成的序列s,你需要找出一個乘積最大的連續子序列
- 最長上升子序列
- 線性dp:最長公共子串
- FMC293-基於FMC 16路LVDS輸入或者輸出子卡