hdu 1503 LCS输出路径【dp】】的更多相关文章

hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左走.我用1表示向上走,2向左上方走,3向左走. 刚开始输入字符串时有两种方法,直接输入:或从地址后一位输入,即此时数组起始编号为1.直接输入时,dp[i][j]表示的是以s1[i-1],s2[j-1]为结尾LCS,另一种则就是表示以s1[i],s2[j]为结尾的LCS.两者在路径输出时有些差别,以前…
题意:有两个字符串,求他们的最长公共子序列并输出. 题解:首先跑个LCS记录一下dp数组,然后根据dp数组来反着还原路径,只有当两个位置的字符相同时才输出. 代码: char s[N],t[N]; int dp[10000][10000]; int main() { //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); scanf("%s %s",s+1,t+1); int len1=strlen(s+1); int len2=st…
http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时间1, 遇到有数字的格子还要花费这个数字大小的时间,输出最后走到(n-1,m-1)的最小时间,还要输出他的路径,'X'是墙,‘.’是可以走的空地 就是这个路径,刚一看题看到那个样例输出吓得我都飞起来了,好多啊! 其实还好啦,广搜,一开始还因为是普通的广搜,后来发现不一样, 为了寻求最小时间,他可以选…
Floyd 输出路径 Sample Input50 3 22 -1 43 0 5 -1 -122 5 0 9 20-1 -1 9 0 44 -1 20 4 05 17 8 3 1 //收费1 3 //起点 终点3 52 4-1 -10 Sample OutputFrom 1 to 3 :Path: 1-->5-->4-->3Total cost : 21 From 3 to 5 :Path: 3-->4-->5Total cost : 16 From 2 to 4 :Path…
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 这道题又WA了好几次 在裸最长公共子串基础上加了回溯功能,就是给三种状态各做一个 不同的标记.dp[n][m]开始回找,找到这条最长串的组成. WA点有几个都被我遇到了 一个是最长公共串为0时,两个串直接输出 一个是最长公共串为1时,后续串的处理 这里要记得是dp回溯的方式 #include<iostream> #include<cstdio> #include<algorithm&…
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…
题意: 将两个英文单词进行合并.[最长公共子串只要保留一份] 输出合并后的英文单词. 思路: 求最长公共子串. 记录路径: 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){…
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][…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 题目大意: 给两个字符串,组成一个长度尽可能小的字符串,它包含上述两个字符串,且原字符串中的字符在该串中的相对位置不变. Sample Input apple peach ananas banana pear peach   Sample Output appleach bananas pearch   解题思路:要结合样例来理解题意,本题主要难在如何输出题目要求字符串,这就需要我们仔细研究样…