POJ1458 Subsquence】的更多相关文章

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 strictly increasing…
什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5}, {1} 其中最长的递增子序列是{2, 4, 5} 问题:对于长度为N的矢量D,如何找到它的最长递增子序列 一个简单的算法 . 找到所有长度为i的子序列; //复杂度为(N!)/(i!)(N-i)! O(exp(N)) . 判断是否其中有一个为递增子序列} 动态规划算法 基本思想:将一个复杂问题…
转载自:https://www.cnblogs.com/huashanqingzhu/p/7423745.html 题目链接:http://poj.org/problem?id=1458 题目大意:给出两个字符串,求出这样的一个最长的公共子序列的长度:子序列中的每个字符都能在两个原串中找到,而且每个字符的先后顺序和原串中的先后顺序一致. 输入有若干行,每行是两个字符串.对每一行输入的两个字符串,输出最长公共子串的长度. Sample Inputabcfbc abfcabprogramming c…
题目链接:http://poj.org/problem?id=1458 题目大意:给出两个字符串,求出这样的一个最长的公共子序列的长度:子序列中的每个字符都能在两个原串中找到,而且每个字符的先后顺序和原串中的先后顺序一致. 输入有若干行,每行是两个字符串.对每一行输入的两个字符串,输出最长公共子串的长度. Sample Inputabcfbc abfcabprogramming contest abcd mnp Sample Output420 算法分析 参考1:北大郭炜老师mooc课程参考2:…
POJ1159,动态规划经典题目,很适合初学者入门练手. 求:为了使字符串左右对称,应该插入的最小字符数目. 设字符串为S1 S2 S3 - Sn. 这个字符串有n个字符,根据DP的基本思路,减少问题规模.如果S1和Sn匹配,则只关心S2 S3 -Sn-1,就这样问题规模减少了.如果S1和Sn不匹配,那就有两种办法. 方法1:加入S1',字符串成S1S2 S3 - Sn S1',则问题转化为S2 S3 - Sn. 方法2:加入Sn',字符串成Sn'S1S2 S3 - Sn,则问题转化为S1 S3…
Common Subsequence POJ-1458 //最长公共子序列问题 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<string> using namespace std; string a,b; int dp[1003][1003]; int main(){ while(cin>>a>>b){…
link:http://poj.org/problem?id=1458 最基础的那种 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cctype> #include <algorithm> #include <queue> #include <de…
//Accepted 4112 KB 16 ms //最长公共子串 #include <cstdio> #include <cstring> #include <iostream> using namespace std; ; int dp[imax_n][imax_n]; char s1[imax_n]; char s2[imax_n]; int len1,len2; int max(int a,int b) { return a>b?a:b; } void D…
Problem 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 stri…
Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45763   Accepted: 18737 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 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37551   Accepted: 15023 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 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37614   Accepted: 15058 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<map> #include<set> #include<list> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define…
给出两个字符串,求出这样的一个最长的公共子序列的长度:子序列中的每个字符都能在两个原串中找到,而且每个字符的先后顺序和原串中的先后顺序一致. Sample Input: abcfbc abfcabprogramming contestabcd mnp Sample Output:420 #include<iostream> #include<algorithm> using namespace std; ]; ]; ][]; int main(void) { while (cin…
什么是最长公共子序列 X=ACCG Y=CCAGCA 长度为1的公共子序列: {A} {C} {G} 长度为2的公共子序列:{AC} {CC} {CG} {AG} 长度为3的公共子序列:{ACG} 长度为4的公共子序列 最长公共子序列即为 {ACG} 问题:长度为N和M的两个序列如何求他们的最长公共子序列? X = ACCGGGTTACCGTTTAAAACCCGGGTAACCT Y = CCAGGACCAGGGACCGTTTACCAGCCTTAAACCA 简单算法 for (int i=N; i…
本题大意:给出两个字符串,让你求出最长公共子序列的长度并输出. 本题思路:本题是经典的DP问题,由于是两个字符串,那么我们就用一个二维数组来进行区分,用dp[ i ][ j ]来表示在s1和s2中分别以i, j 结尾的子串中最长公共子序列的长度,很容易得出dp[i + 1] [j + 1] 的值只与dp[ i ][ j ] ,dp[i + 1][ j ] , dp[[ i ][ j + 1]的值有关,那么我们就可以列出状态转移方程. dp[ i + 1][j + 1] = s1[ i ] ==…
Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 51319   Accepted: 21146 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 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44464 Accepted: 18186 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…
描述: 给定两个字符串,求其最长公共子序列(不用连续), 输入: abc bcc programning content 输出: 2 2 解法: 动态规划. 定义dp[i][j]表示s1到i索引,以及s2到j索引为止的最长公共子序列, 则定义如下: dp[i][j] = dp[i - 1][j - 1] + 1, 若s1[i] == s2[j] max(dp[i - 1][j], dp[i][j - 1]), 若s1[i] != s2[j] 遍历此二维数组即可,时间复杂度为O(m*n),即两字符…
此题经典线性动态规划. 代码如下: #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…
题意与分析 很简单:求最长公共子序列. 注意子序列与子串的差别:一个不连续一个连续.一份比较好的参考资料见:https://segmentfault.com/a/1190000002641054 状态转移方程是这样的: 设dp[i][j]dp[i][j]为a串1~i.b串1~j中的最长的公共子序列,则 dp[i][j]={dp[i−1][j−1]+1,  max(dp[i−1][j],dp[i][j−1]),  a[i]=b[j],a[i]≠b[j]dp[i][j]={dp[i−1][j−1]+…
Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44223   Accepted: 18088 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..…
最长回文子序列可以用求解原串s和反转串rv的LCS来得到,因为要求回文串分奇偶,dp[i][j]保存长度, 要求字典序最小,dp[i][j]应该表示回文子序列的端点,所以边界为单个字符,即i+j=len+1. 这题最麻烦的地方在于字典序,我是写了个比较函数,有点暴力(常数大). 也可以反着定义,这时结点就要保存那个状态的字符串了(这样定义比较字典序的时候常数小) #include<bits/stdc++.h> using namespace std; #define MP make_pair…
基础DP. #include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm> using nam…
题目链接: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…
/*Common SubsequenceTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 56416 Accepted: 23516DescriptionA subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm…
题目链接:http://poj.org/problem?id=1458 分析:最大公共子序列模板 1 #include<iostream> 2 #include<sstream> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<string> 6 #include<cstring> 7 #include<algorithm> 8 #include<functiona…
一个sequence,里面都是整数,求最长的subsequence的长度,使得这个subsquence的最大值和最小值相差不超过1. 比如[1,3,2,2,5,2,3,7]最长的subsequence是[3,2,2,2,3],所以应该返回5. 分析: 这题可以先排序,然后找出两个相差1的相邻值的最大长度.第二种方法可以用HashMap,保存每个值出现的个数,然后从当前值往下找比当前值大1的数出现的个数. public int maxSequence(int[] arr) { ) ; Map<In…
实现如下: public static void main(String[] args) { String squence1 = "ABCBDAB"; String squence2 = "BDCABC"; int len1 = squence1.length(), len2 = squence2.length(); if (len1 <= 0 || len2 <= 0) { System.out.println("empty squence!…
常见优化 单调队列 形式 dp[i]=min{f(k)} dp[i]=max{f(k)} 要求 f(k)是关于k的函数 k的范围和i有关 转移方法 维护一个单调递增(减)的队列,可以在两头弹出元素,一头压入元素. 队列中维护的是两个值.一个是位置,这和k的范围有关系,另外一个是f(k)的值,这个用来维护单调性,当然如果f(k)的值可以利用dp值在O(1)的时间内计算出来的话队列中可以只维护一个表示位置的变量. 枚举到一个i的时候,首先判断队首元素的位置是否已经不满足k的范围了,如果不满足就将队首…