LCS和输出 hdu 1503(Advanced Fruits)
来源:互联网 发布:如何在淘宝店发通告 编辑:程序博客网 时间:2024/06/07 09:52
Advanced Fruits
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 peachananas bananapear peach
Sample Output
appleachbananaspearch
题意:给两个单词,组合成一个,公共部分只输出1次。
题解:LCS 变形。输出注意用了回溯。在LCS时加上标记。
<span style="font-size:18px;">#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char a[110];char b[110];int t;int c[110][110];//标记int dp[110][110];//前i个a与前j个b组成的LCS长度void lcsput(int i,int j){//回溯输出if (!i &&!j)return ;if (c[i][j]==0){//公共部分lcsput(i-1,j-1);//回溯printf ("%c",a[i-1]);}else if (c[i][j]==1){//a的特有元素lcsput(i-1,j);printf ("%c",a[i-1]);}else{//b的特有元素lcsput(i,j-1);printf ("%c",b[j-1]);}}int main(){while (scanf ("%s %s",a,b)!=EOF){int i,j;int len1=strlen(a);int len2=strlen(b);memset (c,0,sizeof(c));memset (dp,0,sizeof(dp));for (i=0;i<len1;i++)c[i][0]=1;for (i=0;i<len2;i++)c[0][i]=-1;for (i=1;i<=len1;i++){for (j=1;j<=len2;j++){if (a[i-1]==b[j-1]){dp[i][j]=dp[i-1][j-1]+1;c[i][j]=0;}else if (dp[i-1][j]>=dp[i][j-1]){c[i][j]=1;dp[i][j]=dp[i-1][j];}else{c[i][j]=-1;dp[i][j]=dp[i][j-1];}}}lcsput(len1,len2);printf ("\n");}return 0;} </span>
类似题: poj Compromise 题解(大神)
0 0
- LCS和输出 hdu 1503(Advanced Fruits)
- HDU 1503 Advanced Fruits(LCS+输出路径)
- HDU:1503 Advanced Fruits(LCS+标记路径+输出)
- HDU 1503 Advanced Fruits(LCS+输出路径~)
- hdu 1503 Advanced Fruits(LCS)
- HDU 1503 Advanced Fruits (LCS,DP)
- hdu 1503 Advanced Fruits (LCS)
- HDU 1503 Advanced Fruits (LCS)
- HDU-1503 Advanced Fruits(DP LCS)
- HDU 1503 Advanced Fruits[ LCS ]
- hdu 1503 Advanced Fruits (LCS)
- HDU 1503--Advanced Fruits【LCS】
- HDU 1503 Advanced Fruits(LCS)
- HDU 1503 Advanced Fruits LCS -
- HDU 1503 Advanced Fruits【LCS】
- HDU-1503 Advanced Fruits(LCS)
- HDU 1503 Advanced Fruits(LCS变形且输出解)
- 【DP|LCS+输出路径】HDU-1503 Advanced Fruits
- 【NOIP提高】通讯
- 浅谈Android中的PendingIntent
- java ----- 反射
- HUST 1352 Repetitions of Substrings
- poj2240——Arbitrage(Bellman-Ford算法)
- LCS和输出 hdu 1503(Advanced Fruits)
- vim without extern
- DataGuard主备之间解决gap的步骤
- 构造数据类型简单介绍
- C# Control的Invoke和BeginInvoke
- JSP动作元素
- HDU 5826(水题,物理公式推导,积分公式推导直接出结果)
- 动态规划 - 九度OJ 1480
- css 利用阴影给字体加粗