1808:公共子序列 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 我们称序列Z = < z1, z2, ..., zk >是序列X = < x1, x2, ..., xm >的子序列当且仅当存在 严格上升 的序列< i1, i2, ..., ik >,使得对j = 1, 2, ... ,k, 有xij = zj.比如Z = < a, b, f, c > 是X = < a, b, c, f, b, c >…
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…
题目传送门 题意:输出两字符串的最长公共子序列长度 分析: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…
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[…
解题思路:先注意到序列和串的区别,序列不需要连续,而串是需要连续的,先由样例abcfbc         abfcab画一个表格分析,用dp[i][j]储存当比较到s1[i],s2[j]时最长公共子序列的长度 a    b    f    c    a    b 0    0    0    0    0   0    0 a  0    1     1    1    1   1    1 b  0    1     2    2    2   2    2 c  0    1     2  …
题目链接Time Limit: 1000MS Memory Limit: 10000K Total Submissions: Accepted: 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 = &…
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…
题目大意:求两个字符串的最长公共子序列 题目思路:dp[i][j] 表示第一个字符串前i位 和 第二个字符串前j位的最长公共子序列 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<iostream> #include<algorithm> #define INF 0x3f3f3f3f #define MAXSIZE 10…
题意:给定两个字符串,让你找出它们之间最长公共子序列(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)}…
题目传送门 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…
Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46387   Accepted: 19045 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..…
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=1458 思路分析:经典的最长公共子序列问题(longest-common-subsequence proble),使用动态规划解题. 1)问题定义:给定两个序列X=<X1, X2, ...., Xm>和Y = <Y1, Y2, ...., Yn>,要求求出X和Y长度最长的最长公共子序列: 2)问题分析: <1>动态规划问题都是多阶段决策最优化问题:在这些问题中,问题可以被划分为多个阶段,每个阶段都需要作出一…
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, ..…
一.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…
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, ..…
虽然以前可能接触过最长公共子序列,但是正规的写应该还是第一次吧. 直接贴代码就好了吧: #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…
Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46630   Accepted: 19154 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..…
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…
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 ; 否则…
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…
Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength(vector<int>& A, vector<int>& B) { int len1 = A.size(); int len2 = B.size(); || len2 == ) ; vector<vector<,vector<)); ;i <=…
题目 同:ZJU 1733,HDU 1159 #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; ][]; int main() { ], b[]; int la, lb; while(scanf("%s%s", a, b) != EOF) { memset(dp, , sizeof(dp)); la = strlen(a), lb = st…
题意:略 求最长公共子串 #include<iostream> #include<cstdio> #include<string> using namespace std; int dp[500][500]; int max(int a,int b) { return a>b?a:b; } int main() { int i,j,a,b; char s1[400],s2[400]; while(scanf("%s%s",s1,s2)!=EOF…
最简单的LIS; 设字符串为 a = acc  b = cc 则dp数组为 0 0 1 1 1 2 b[0] = a[1], b[1] = a[1] 防止这里算两个 要清楚的是 怎么不重复计算 也就是dp[1]的计算 首先dp[1][0] = 1; 再处理 dp[1][1], 因为 b[1] = a[1], 它是可以加一的 加哪一个呢? 要是直接继承左边的 选择dp[1][0] + 1 就会发生重复计算 因为b[0] = a[1], dp[1][0]已经是加上了一个共同字符的值 解决方法是 选择…
最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合. Problem 求数列 a[1..n], b[1..m]的LICS的长度, a[], b[]数组的元素均为正整数. Solution 考虑如何定义DP状态,定义DP状态就是定义所谓的最优子问题(optimal subproblem),而DP状态要能转移,就是所谓最优子问题要具有重叠子结构. 将DP状态定义为 DP[i][j]:a[1..i], b[…
最长公共子序列(LCS) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定两个字符串s,t, 求出这两个字符串最长的公共子序列的长度. 字符串的子序列并一定要连续, 能够包含间隔. 即最长公共子序列问题(LCS, Longest Common Subsequence) 使用动态规划, 假设字符相等, 两个字符串就依次递增一位, 一直到字符串的结尾. 代码: /* * main.cpp * * Created on: 2014.7.17…
给定两个字符串S和T.求出这两个字符串最长的公共子序列的长度. 输入: n=4 m=4 s="abcd" t="becd" 输出: 3("bcd") 这类问题被称为最长公共子序列问题(LCS,Longest Common Subsequence)的著名问题. max(dp[i][j]+1,dp[i][j+1],dp[i+1][j])   (s=t) dp[i+1][j+1]= max(dp[i][j+1],dp[i+1][j])        …
题目链接:http://poj.org/problem?id=1458 Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55099   Accepted: 22973 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left ou…
最长公共子串 // Longest Common Subsequence 子串有别于子序列, 子串是连续的, 而子序列可以不连续 /*---------------------------------------------------- 题为求 最长对称子串, 实际可以转化成求最长公共子串 ----------------------------------------------------*/// 首先,是最长公共子串,而非子序列. // 进而,对称子串,相当于求某个字符串和其逆序的公共子…