poj 1934(LCS)】的更多相关文章

转自: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中最后一…
[题目链接] http://poj.org/problem?id=1934 [算法] 先用dp求出LCS,然后搜索即可,注意加上一些剪枝 [代码] #include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include…
compromise Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is…
Advanced Fruits Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2158   Accepted: 1066   Special Judge Description The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit…
Secretary Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1655   Accepted: 671 Description The basic condition of success of a political party, it is the good Election Programme. PSOS know about it, so they entrust the top secretary Juli…
$ 2019 $ 暑期刷题记录: $ POJ~1952~~BUY~LOW, BUY~LOWER: $ (复杂度优化) 题目大意:统计可重序列中最长上升子序列的方案数. 题目很直接的说明了所求为 $ LIS $ 的方案数 但是题目给出的元素是会重复的,结果需要去重 用 $ n^2 $ 的动态规划再另建数组记录方案可以秒杀 如果要优化复杂度就必须用求 $ LIS $ 的 $ nlogn $ 算法,其中树状数组符合要求 $ POJ~1934~~Trip: $ (方案输出) 题目大意:输出两个序列中最长…
题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文子串长度.对于求原串最长回文子串长度用的是DP的经典问题LCS最长公共子序列的做法. 设原串为S,原串的逆串为S‘,那么原串的最长回文子串长度=S和S'的最长公共子序列长度. 由于N的范围最大是5000,所以5000*5000的数组开不下,所以需要用到滚动数组来求.[关于原串求最长回文子串用的Man…
题目传送门 题意:输出两字符串的最长公共子序列长度 分析: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…
题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 最长公共子串的长度. 额,,这个思路还是不是很好想. LCS: #include<iostream> #include<cstring> #include<cstdio> using namespace std; +; char s1[maxn], s2[maxn]; ][…
POJ 2250 Compromise(LCS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/D 题目: Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulf…
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…
题目链接: http://poj.org/problem?id=3356 AGTC Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13855   Accepted: 5263 Description Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operat…
题目链接: http://poj.org/problem?id=2250 Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9284   Accepted: 3972   Special Judge Description In a few months the European Currency Union will become a reality. However, to join the clu…
题目链接: 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…
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[…
任意门:http://poj.org/problem?id=1159 解题思路: LCS + 滚动数组 AC code: #include <cstdio> #include <iostream> #include <algorithm> #define INF 0x3f3f3f3f #define LL long long using namespace std; ; char a[MAXN], b[MAXN]; ][MAXN]; int main() { int l…
一.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 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要在该字符串中插入几个字符能是的它变成一个回文串. 分析: 首先把原字符串和它的逆串进行匹配, 找出最长公共子序列. 那么最长公共子序列的字符串肯定是一个回文串. 所以原串剩下的部分是不构成回文的. 我们仅仅须要加入剩下部分的字符到相应位置, 原串自然就变成了一个回文. 所以本题的解为: n 减去 (原串与逆串…
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 ; 否则…
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…
题目字符串的LCS,输出解我比较不会,dp的时候记录从哪里转移来的,之后要一步一步转移回去把解存起来然后输出. #include<cstdio> #include<cstring> #define N 102 #define M 32 int n,m,dp[N][N],ans[N][N]; char s1[N][M],s2[N][M],str[N][M]; void solve(){ memset(dp,0,sizeof dp); memset(ans,0,sizeof ans);…
题目传送门 题意:求单词的最长公共子序列,并要求打印路径 分析:LCS 将单词看成一个点,dp[i][j] = dp[i-1][j-1] + 1 (s1[i] == s2[j]), dp[i][j] = max (dp[i-1][j], dp[i][j-1]) 代码: #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <strin…
题目传送门 题意:一个字符串要变成回文串至少要插入多少个字符 分析:LCS,长度 - 原串和反串的最大相同长度就是要插入的个数.解释一下,当和反串相同时,在原串中已经是回文的部分了,那么减去LCS长度后剩下的多少个插入多少个一定就能使原串回文dp数组二维都开5000的话就会超内存,这里就用到了滚动数组,因为在LCS的计算中,i的变化只相差1,所以可以通过对2取余来进行滚动:) 收获:原串和反串取LCS能得到部分回文串,推荐继续看:UVA 11404 Palindromic Subsequence…
题目传送门 题意:两个字符串结合起来,公共的字符只输出一次 分析:LCS,记录每个字符的路径 代码: /* LCS(记录路径)模板题: 用递归打印路径:) */ #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <iostream> using namespace std; const int N = 1e2 + 10; cons…
滚动数组 + LCS // File Name: 1159.cpp // Author: Missa_Chen // Created Time: 2013年07月08日 星期一 10时07分13秒 #include <iostream> #include <string> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> #includ…
Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 55018   Accepted: 19024 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a…
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…
题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output 2 分析:这道题目之前有做过,就是将原字符串逆序得到另一个字符串,求它们的最长公共子序列,这样就能求得它的可以构成回文的最长字符数,用n减去即为添加最少可使之成为回文的数目. 可恨的是之前一直超内存,看了别人的解题报告,原来定义dp[MAX][MAX]时,不用int型,而是short型,内存只占in…
题目链接:http://poj.org/problem?id=1080 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; ; const int INF = 0x3f3f3f; int dp[maxn][maxn]; int A[maxn],B[maxn]; ][] = { {, , , , , }, {,,-,-,-,…
详细解题报告可以看这个PPT 这题如果是直接开int 5000 * 5000  的空间肯定会MLE,优化方法是采用滚动数组. 原LCS转移方程 : dp[i][j] = dp[i - 1][j] + dp[i][j -1] 因为 dp[i][j] 只依赖于 dp[i - 1][j] 和 dp[i][j - 1] 所以可以采用滚动数组如下: dp[i % 2][j] = dp[(i - 1) % 2][j] + dp[i % 2][j - 1] 可以实现节省空间的方法 答案存储在 dp[n % 2…