HDU 1503 Advanced Fruits (LCS,变形)】的更多相关文章

题意: 给两个水果名,要求他们的LCS部分只输出1次,其他照常输出,但是必须保持原来的顺序! 思路: 求LCS是常规的,但是输出麻烦了,要先求LCS,再标记两串中的所有LCS字符,在遇到LCS字符时,先输串1的,再输串2的,然后输该字符,以保证每个LCS字符输出前,两串中该字符前面的字符全部已输出. //#pragma comment(linker,"/STACK:102400000,102400000") #include <iostream> #include <…
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…
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1360    Accepted Submission(s): 672Special Judge Problem Description The company "21st Century Fruits" has specialized in cre…
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3340    Accepted Submission(s): 1714Special Judge Problem Description The company "21st Century Fruits" has specialized in cr…
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1426    Accepted Submission(s): 719Special Judge Problem Description   The company "21st Century Fruits" has specialized in c…
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3613    Accepted Submission(s): 1867 Special Judge Problem Description The company "21st Century Fruits" has specialized in…
http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是要我们求LCS,记录好路径就好. #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<sstream> #include<vect…
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…
题意:给定两个字符串,让你求一个最短的字符串,并且这个字符串包含给定的两个. 析:看到这个题,我知道是DP,但是,不会啊...完全没有思路么,我就是个DP渣渣,一直不会做DP. 最后还是参考了一下题解,主要是这样的,要想最短,就必须让两个字符串重复的最多,也就是LCS, 然后把剩下的不相同的字符再给补上,说起来容易,实现起来,难!反正对我来说是难. 然后根据LCS的原理,我们对这个两字符串,进行标记,什么意思呢,就是说,如果某个字符是公共字符, 等到输出时,我们只输出一次,而对于不是公共的,我们…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 思路:这是一道最长公共子序列的题目,当然还需要记录路径.把两个字符串的最长公共字串记录下来,在递归回溯输出的时候,要是两个字符是公共子串,就只输出一次,要不是,就分别把位于相同位置的两个字符串的字符输出....... #include<cstdio> #include<iostream> #include<algorithm> #include<math.h&g…
题意: 将两个英文单词进行合并.[最长公共子串只要保留一份] 输出合并后的英文单词. 思路: 求最长公共子串. 记录路径: mark[i][j]=-1:从mark[i-1][j]转移而来. mark[i][j]=0:从mark[i-1][j-1]转移而来. mark[i][j]=1:从mark[i][j-1]转移而来. 代码: char s1[105], s2[105]; int dp[105][105]; int mark[105][105]; void print(int x,int y){…
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…
感觉就是最长公共子序列的一个变形(虽然我也没做过LCS啦= =). 转移方程见代码吧.这里有一个要说的地方,如果a[i] == a[j]的时候,为什么不需要像不等于的时候那样减去一个dp[i-1][j-1]呢?其实是要减去的,然后我们注意+1是什么呢?这两个位置是相同的,那么这一对组合是1,然后包含这一个,在dp[i-1][j-1]中相同的又可以拿出来加一遍了,因此就抵消了~ 代码如下: #include <stdio.h> #include <algorithm> #includ…
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 frui…
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2358    Accepted Submission(s): 1201Special Judge Problem Description The company "21st Century Fruits" has specialized in cr…
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2052    Accepted Submission(s): 1053Special Judge Problem Description The company "21st Century Fruits" has specialized in cr…
链接:HDU-1053:Advanced Fruits 题意:将两个字符串合成一个串,不改变原串的相对顺序,可将相同字母合成一个,求合成后最短的字符串. 题解:LCS有三种状态转移方式,将每个点的状态转移方式记录下来,再回溯. #include <bits/stdc++.h> using namespace std; ; const int INF = 0x3f3f3f3f; ; + ; char s[maxn], t[maxn]; int slen, tlen; int dp[maxn][m…
hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左走.我用1表示向上走,2向左上方走,3向左走. 刚开始输入字符串时有两种方法,直接输入:或从地址后一位输入,即此时数组起始编号为1.直接输入时,dp[i][j]表示的是以s1[i-1],s2[j-1]为结尾LCS,另一种则就是表示以s1[i],s2[j]为结尾的LCS.两者在路径输出时有些差别,以前…
Human Gene Functions 题意: LCS: 设dp[i][j]为前i,j的最长公共序列长度: dp[i][j] = dp[i-1][j-1]+1;(a[i] == b[j]) dp[i][j] = max(dp[i][j-1],dp[i-1][j]); 边界:dp[0][j] = 0(j<b.size) ,dp[i][0] = 0(i< a.size); LCS变形: 设dp[i][j]为前i,j的最大价值: value(x, y)为比较价值: dp[i][j] = max(d…
题目链接: http://poj.org/problem?id=1080 Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20430   Accepted: 11396 Description It is well known that a human gene can be considered as a sequence, consisting of four nucleoti…
Advanced Fruits Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1944   Accepted: 967   Special Judge Description The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit…
Color Length(UVA-1625)(DP LCS变形) 题目大意 输入两个长度分别为n,m(<5000)的颜色序列.要求按顺序合成同一个序列,即每次可以把一个序列开头的颜色放到新序列的尾部. https://odzkskevi.qnssl.com/a68cbd3e27f46b4f02ea12b7b1a1abca 然后产生的新序列中,对于每一个颜色c,都有出现的位置,L(c)表示最小位置和最大位置之差,求L(c)总和最小的新序列. 分析 LCS 是公共上升子序列,在动态转移的过程中,考虑…
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…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 题目大意: 给两个字符串,组成一个长度尽可能小的字符串,它包含上述两个字符串,且原字符串中的字符在该串中的相对位置不变. Sample Input apple peach ananas banana pear peach   Sample Output appleach bananas pearch   解题思路:要结合样例来理解题意,本题主要难在如何输出题目要求字符串,这就需要我们仔细研究样…
a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to determine the LCS, not only the length, but all of LCS candidate, we can backtrack to find all of LCS. for backtrack, one criteria is dp[i-1][j]==dp[i][…
题目传送门 题意:两个字符串结合起来,公共的字符只输出一次 分析:LCS,记录每个字符的路径 代码: /* LCS(记录路径)模板题: 用递归打印路径:) */ #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <iostream> using namespace std; const int N = 1e2 + 10; cons…
Human Gene Functions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3008    Accepted Submission(s): 1701 Problem Description It is well known that a human gene can be considered as a sequence,…
反恐训练营 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4064    Accepted Submission(s): 967 Problem Description 当 今国际反恐形势很严峻,特别是美国“9.11事件”以后,国际恐怖势力更是有恃无恐,制造了多起骇人听闻的恐怖事件.基于此,各国都十分担心恐怖势力会对 本国社会造成的不稳…
题意:给定三个字符串,问你第三个是不是由第一个和第二个组成的. 析:当时比赛是没有做出来啊...一直WA,就是没有判断长度,第一个和第二个和是不是和第三个一样,这个忘记... 我们用d[i][j]表示第一个字符串匹配到 i, 第二个匹配到第 j 个,然后只要判断能不能由上一个得到就好.这个主要是d[i][j]==1则表示可以成功匹配 d[i][j]==0则表示无法成功匹配,那么剩下的就简单了. 代码如下: #include <cstdio> #include <string> #i…
http://acm.hdu.edu.cn/showproblem.php?pid=1503 这道题又WA了好几次 在裸最长公共子串基础上加了回溯功能,就是给三种状态各做一个 不同的标记.dp[n][m]开始回找,找到这条最长串的组成. WA点有几个都被我遇到了 一个是最长公共串为0时,两个串直接输出 一个是最长公共串为1时,后续串的处理 这里要记得是dp回溯的方式 #include<iostream> #include<cstdio> #include<algorithm&…