POJ-1458 LCS(线性动态规划)】的更多相关文章

题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; (s[i] == t[i])dp[i+1][j+1] = max (dp[i][j+1], dp[i+1][j]); (s[i] != t[i]) 代码: #include <cstdio> #include <cstring> #include <iostream> #in…
POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/F 题目: Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43388   Accepted: 17613 Description A subsequen…
此题经典线性动态规划. 代码如下: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; ; int d[maxn][maxn]; int main(void){ string a,b; while(cin>>a>>b){ memset(d,,sizeo…
题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there ex…
POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sample Input : abcfbc abfcab programming contest abcd mnp Sample Output 4 2 0 分析: 输入两个串s1,s2, 设dp(i,j)表示: s1的左边i个字符形成的子串,与s2左边的j个 字符形成的子串的最长公共子序列的长度(i,j从…
题意是说,给定一个字符串,问至少还需要插入多少个字符才能使得该字符串成为回文字符串. 这道题一开始做的时候用了一个简单的动态规划,开了一个5000*5000的数组,用递归形式实现,代码如下: 其中d[i][j]表示i到j所需要插入的字符数.然而数组开得太大直接报MLE.因此想到用滚动数组来解决. MLE代码如下: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> u…
Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65333   Accepted: 27331 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..…
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列长度. 分析: 本题不用输出子序列,非常easy,直接处理就可以. 首先令dp[i][j]==x表示A串的前i个字符和B串的前j个字符的最长公共子序列长度为x. 初始化: dp全为0. 状态转移: IfA[i]==B[j] then dp[i][j]= dp[i-1][j-1]+1 else dp[…
一.Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strict…
POJ:http://poj.org/problem?id=1458 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=733 HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1159 题目大意: 给定两串子序列,求最长的公共字串(LCS) 设d( i , j)为A和 B的LCS的长度,则当A[i] = B[j]时, d(i , j)= d(i-1, j-1)+1 ; 否则…
Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a s…
LCS #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define clc(a,b) memset(a,b,sizeof(a)) #define LL long long #include<cmath> using namespace std; ][];//表示到i-1,j-1的最长公共长度 int main() { // freopen(&q…
经典的最长公共子序列问题. 状态转移方程为 : if(x[i] == Y[j]) dp[i, j] = dp[i - 1, j - 1] +1 else dp[i, j] = max(dp[i - 1], j, dp[i, j - 1]); 设有字符串X和字符串Y,dp[i, j]表示的是X的前i个字符与Y的前j个字符的最长公共子序列长度. 如果X[i] == Y[j] ,那么这个字符与之前的LCS 一定可以构成一个新的LCS: 如果X[i] != Y[j] ,则分别考察 dp[i  -1][j…
题意:给定两个字符串,让你找出它们之间最长公共子序列(LCS)的长度. 析:很明显是个DP,就是LCS,一点都没变.设两个序列分别为,A1,A2,...和B1,B2..,d(i, j)表示两个字符串LCS长度. 当A[i] = B[j] 时,这个最长度就是上一个长度加1,即:d(i, j) = d(i-1, j-1) + 1; 当A[i] != B[j] 时,那就是前面的最长长度(因为即使后面的不成立,也不会影响前面的),即:d(i, j) = max{d(i-1, j), d(i, j-1)}…
Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: 17472 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..…
Common Subsequence Descriptions: A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if th…
虽然以前可能接触过最长公共子序列,但是正规的写应该还是第一次吧. 直接贴代码就好了吧: #include <stdio.h> #include <algorithm> #include <string.h> using namespace std; + ; char a[N],b[N]; int dp[N][N]; int main() { ,b+) == ) { ); ); memset(dp,,sizeof dp); ;i<=n;i++) { ;j<=m…
#include <iostream> #include <algorithm> #include <string> #include <cstring> #include <cstdio> #define MAX 1005 using namespace std; int ans[MAX][MAX]; int main(){ string s1,s2; while(cin>>s1>>s2) { memset(ans,,s…
题目链接:http://poj.org/problem?id=1458 思路分析:经典的最长公共子序列问题(longest-common-subsequence proble),使用动态规划解题. 1)问题定义:给定两个序列X=<X1, X2, ...., Xm>和Y = <Y1, Y2, ...., Yn>,要求求出X和Y长度最长的最长公共子序列: 2)问题分析: <1>动态规划问题都是多阶段决策最优化问题:在这些问题中,问题可以被划分为多个阶段,每个阶段都需要作出一…
求最长公共子序列LCS,用动态规划求解. UVa的字符串可能含有空格,开始用scanf("%s", s);就WA了一次...那就用gets吧,怪不得要一行放一个字符串呢. (本来想用fgets的,可是又放弃了,形式麻烦.代码长是一小方面,另一方面fgets把'\n'字符也读入,还要做额外的处理...虽然gets有传说中的缓冲区溢出漏洞,不过多加注意一下就好啦,个人认为代码还没大到要用那些工程性的东西的时候) #include <cstdio> #include <cs…
http://poj.org/problem?id=1458 用dp[i][j]表示处理到第1个字符的第i个,第二个字符的第j个时的最长LCS. 1.如果str[i] == sub[j],那么LCS长度就可以+1,是从dp[i - 1][j - 1] + 1,因为是同时捂住这两个相同的字符,看看前面的有多少匹配,+1后就是最大长度. 2.如果不同,那怎么办? 长度是肯定不能增加的了. 可以考虑下删除str[i] 就是dp[i - 1][j]是多少,因为可能i - 1匹配了第j个.也可能删除sub…
http://poj.org/problem?id=1458 一道容易的DP,求最长公共子序列的 dp[i][j],代表str1中的第i个字母和str2中的第j个字母的最多的公共字母数 #include <stdio.h> #include <iostream> #include <string.h> using namespace std; ][]={}; int main() { ],str2[]; while(~scanf("%s %s",st…
转自:http://www.cppblog.com/varg-vikernes/archive/2010/09/27/127866.html 1)首先按照常规的方法求出最长公共子序列的长度也就是用O(MN)的那个动态规划,结果放在二维数组dp里dp[i][j] = { 字串a的1~i部分与字串b的1~j部分的最长公共子序列的长度 }2)求辅助数组last1[i][j] = { 到下标i为止,字符j在字串a中最后一次出现的下标 }last2[i][j] = { 到下标i为止,字符j在字串b中最后一…
1.链接地址: http://poj.org/problem?id=1458 http://bailian.openjudge.cn/practice/1458/ 2.题目: Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35411   Accepted: 14080 Description A subsequence of a given sequence is the given…
题目链接:http://poj.org/problem?id=3356 思路分析:题目为经典的编辑距离问题,其实质为动态规划问题: 编辑距离问题定义:给定一个字符串source,可以对其进行复制,替换,删除,增加操作,另外根据具体情况已经规定了每种操作的cost,现在要求求出一个操作序列,使其变为一个给定的字符串dest,并且该操作序列的cost的和最小(在该题目中复制开销为0,其他开销为1): 该问题为动态规划问题,先对该问题进行分析: 1)发掘最优子结构: 假设源字符串为S[0, 1, 2,…
http://poj.org/problem?id=1458 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence…
任务说明:这也是基础的动态规划.是在线性结构上面的动态规划,一定要掌握. P1020 导弹拦截 导弹拦截 P1091 合唱队形 老师给同学们排合唱队形.N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2…,K,他们的身高分别为T1,T2,…,TK, 则他们的身高满足T1<...<Ti>Ti+1>…>TK(1<=i<=K). 解答:前面是LIS, 后面是LDS,枚…
题目链接:http://poj.org/problem?id=3211 题意:有M件衣服,每种衣服有一种颜色,一共有N种颜色.现在两个人洗衣服,规则是必须把这一种颜色的衣服全部洗完才能去洗下一种颜色的衣服. 问:在两个人可以同时洗衣服的情况下,把衣服全部洗完最少需要多久. 如果说两个人同时洗同一种颜色衣服,那么最少的时间就是洗完该颜色衣服的总时间的一半. 那么我们可以将洗每种衣服分开来看,视作一个01背包,容量是洗该颜色衣服的总时间的一半. 然后最多花多久.那么该颜色的总时间-这个人花的最多时间…
题目地址:http://poj.org/problem?id=1661 Description "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度各不相同的平台.地面是最低的平台,高度为零,长度无限. Jimmy老鼠在时刻0从高于所有平台的某处开始下落,它的下落速度始终为1米/秒.当Jimmy落到某个平台上时,游戏者选择让它向左还是向右跑,它跑动的速度也是1米/秒.当Jimmy跑到平台的边缘时,开始继续下落.Jimmy每次下落的高度不能超过MAX米,不…
题目地址:http://poj.org/problem?id=1276 Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1…