hdoj-1503-Advanced Fruits【LCS】

Advanced Fruits

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

Total Submission(s): 2087 Accepted Submission(s): 1072

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

Source

University of Ulm Local Contest 1999

Recommend

linle | We have carefully selected several similar problems for you:
1502 1501 1227 1158 1078

#include<stdio.h>
#include<string.h>
int dir[110][110],dp[110][110];
char s1[110],s2[110];
int len1,len2;
void out(int x,int y){
	//printf("%d##\n",dir[x][y]);
	if(x==0&&y==0) {
		return;
	}
	if(dir[x][y]==0) {
	    out(x-1,y-1);
		printf("%c",s1[x]);
	}
	if(dir[x][y]==1){
	    out(x-1,y);
		printf("%c",s1[x]);
	}
	if(dir[x][y]==-1) {
	    out(x,y-1);
		printf("%c",s2[y]);
	}
}
int main(){
	while(~scanf("%s%s",s1+1,s2+1)){
		memset(dp,0,sizeof(dp));
		int i,j;
		len1=strlen(s1+1); len2=strlen(s2+1);
		for(i=0;i<=len1;++i) dir[i][0]=1;
		for(i=0;i<=len2;++i) dir[0][i]=-1;

		for(i=1;i<=len1;++i){
			for(j=1;j<=len2;++j){
				if(s1[i]==s2[j]){
					dp[i][j]=dp[i-1][j-1]+1;
					dir[i][j]=0;
				}
				else if(dp[i-1][j]>dp[i][j-1]){
					dp[i][j]=dp[i-1][j];
					dir[i][j]=1;
				}
				else{
					dp[i][j]=dp[i][j-1];
					dir[i][j]=-1;
				}
			}
		}
		out(len1,len2);
		printf("\n");
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-10 14:36:31

hdoj-1503-Advanced Fruits【LCS】的相关文章

HDU1503:Advanced Fruits 【LCS】

Advanced Fruits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 2 Special Judge Problem Description The company "21st Century Fruits" has specialized in creating

POJ2264 HDU1503 Advanced Fruits【LCS】

题目链接: http://poj.org/problem?id=2264 http://acm.hdu.edu.cn/showproblem.php?pid=1503 题目大意: 两种水果可以杂交出一种新的水果,现在要给新水果起名字,起名的规则是: 这个名字要包含之前两种水果的名字的字母,要按原本字符串中字符的相对顺序.并且这个 名字要尽可能的短. 思路: 先求出两种水果名字s1和s2最长公共子序列的长度,并且用pre[i][j]标记下dp[i][j]的上一个状态, 来得到每个字符在新的字符串中

HDU 1503 Advanced Fruits (LCS,DP)

题意:给你两字符串s1,s2,用最短的字符串表示他们(公共字串输出一次). Sample Input apple peach ananas banana pear peach Sample Output appleach bananas pearch dp[i][j] : 第一个字符串的前 i 个 ,和第二个字符串的前 j 个最短组合的长度 . pre[i][j] : 第一个字符串的第 i 个 ,和第二个字符串的第 j 个字符的状态. #include<cstdio> #include<

HDU 1503 Advanced Fruits(LCS+记录路径)

http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是要我们求LCS,记录好路径就好. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<sstream> 6 #inc

hdoj 1159 Common Subsequence【LCS】【DP】

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28494    Accepted Submission(s): 12735 Problem Description A subsequence of a given sequence is the given sequence with some e

hdoj 1863 畅通工程 【最小生成树】+【kruskal】

题意:... 难点:如何判断是不是信息不全:在输入的时候建立并查集,之后判断有几个节点就可以了,剩下的就是kruskal算法. 代码: #include<stdio.h> #include<string.h> #include<algorithm> #define MAXN 105 #define INF 0x3f3f3f3f using std::sort; struct node{ int from; int to; int w; }edges[MAXN*MAXN]

hdoj 2391 Filthy Rich 【DP】

题目大意:有个二维数组,你从(0,0)出发,最终到(n,m), 在这个二维数组中,每个位置dp[i][j]都有一定量的黄金,你可以拾取,问你最多能失去多少,并且,你的方向有下,右, 斜向下三个方向: 策略:就是每一个都加上它的上方向与左方向的最大值,这样到最后就是最大值.详情见代码 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2391 代码: #include<stdio.h> #include<string.h> int dp[1

hdoj 1045 Fire Net 【DFS】

题意:如果两个点要放在同一行或者同一列,那么两个点中间要有一个墙,否则的话只能放一个点,最后问你最多能放几个点. 看了一个星期.. 这道题的解法我还是第一次见,就是逐个逐个的来放置每个点,然后每经过一个点都判断一次,详情看代码 代码: #include <stdio.h> #include <string.h> int ans, n; char map[10][10]; int judge(int lin, int row) { int i; for(i = lin-1; i &g

hdoj 4907 Task schedule 【预处理】

题意:中文题,你懂得... 思路:建两个数组,一个标记,一个放答案(就是最快能处理的任务点), 在输入数据的时候标记改位置已经有任务了,并且找出来一个最大的数max.然后从max+1,出发从大到小,依次用temp定义没有任务的序号,如果是没有被标记那么就将该处的答案定义为temp. 题目链接 点击打开链接 代码: #include<stdio.h> #include<string.h> #define MAXN 200005 int vis[MAXN], ans[MAXN]; in