Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在这个串中删除最少的字符,使得它回文是一样的. 那么我们可以把这个串reverse,之后的串称为s2,找s2和s的最长公共子序列就好了,因为有了LCS,接着把其他的都删掉,就是一个回文串了,因为正着读和倒着读都一样 还有POJ居然能跑5000^2 我的923MS就跑完了,还是很快的嘛,当然这题还可以滚动数组,…
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 program which, given a string, determines the minimal number of characters to be inserted into t…
Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56273   Accepted: 19455 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…
Palindrome DescriptionA 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 program which, given a string, determines the minimal number of characters to be inser…
Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 48526   Accepted: 16674 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 program whi…
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…
Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 39661    Accepted Submission(s): 18228 Problem Description A subsequence of a given sequence is the given sequence with some el…
F - F Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u   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 > anothe…
题目描述:给出两个字符串,求两个字符串的公共子序列(不是公共子串,不要求连续,但要符合在原字符串中的顺序) in: abcfbc abfcab programming contest abcd mnp out: 4 2 0 状态转移方程: 如果s1[i]==s2[j] 则 c[i][j]=c[i-1][j-1]+1 如果s1[i]!=s2[j] 则 c[i][j]=max(c[i-1][j],c[i][j-1]) #include <iostream> #include <cstring…
#include <cstdio> #include <cstring> using namespace std; ; #define max(a,b) a>b?a:b char a[N] , b[N]; int dp[N][N]; int main() { , b+) != EOF){ ); ); ; i<=l1 ; i++) ; j<=l2 ; j++){ ][j-]+; ][j] , dp[i][j-]); } printf("%d\n"…
http://poj.org/problem?id=1159 题意: 给出一个字符串,计算最少要插入多少个字符可以使得该串变为回文串. 思路: 计算出最长公共子序列,再用原长-LCS=所要添加的字符数. #include<iostream> #include<string> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; + ; char s1…
Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 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…
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 program which, given a string, determines the minimal number of characters to be inserted into t…
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://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max len of LCS a串匹配到第i位,b串匹配到第j位,此时的最长公共子序列长度. 如何转移: 首先,一个明显的决策是,如果a[i] == b[j],那么此一定要匹配.(贪心) 所以分两种情况: (1)a[i] == b[j]:dp[i][j] = dp[i-1][j-1] + 1 (2)a[i]…
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从…
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://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 37551    Accepted Submission(s): 17206 Problem Description A subsequence of…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25416    Accepted Submission(s): 11276 Problem Description A subsequence of…
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然要使用动态规划了. 这里\(dp[i][j]\)代表第一个串的前\(i\)个字符和第二个串的前\(j\)个字符中最长的公共子序列的最长长度,递推关系如下: \[ d[i][j]= \begin{cases} dp[i-1][j-1]+1 & \text{if} &str1[i]==str2[j…
题目链接 基础的最长公共子序列 #include <bits/stdc++.h> using namespace std; ; char c[maxn],d[maxn]; int dp[maxn][maxn]; int main() { while(scanf("%s%s",c,d)!=EOF) { memset(dp,,sizeof(dp)); int n=strlen(c); int m=strlen(d); ;i<n;i++) ;j<m;j++) if(c…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个最少的个数是多少? 一开始以为只是一个普通的DP题,但是按照我的想法敲出来之后怎么样都W了,无奈搜了解题报告,得知其实这个就是一个最长公共子序列问题,就是求这个字符串跟它的逆序的 字符串的最长公共子序列.因为杭电的题内存都要求在32M内存以内,所以很开心的敲出来才发现10^6的数组都开不了,所以只好…
子序列就是子序列中的元素是母序列的子集,且子序列中元素的相对顺序和母序列相同. 题目要求便是寻找两个字符串的最长公共子序列. dp[i][j]表示字符串s1左i个字符和s2左j个字符的公共子序列的最大长度. 注意s1第i个字符为s1[i-1] 于是有递推公式: 对于abcfbc和abfcab两个字符串,求公共子串的最大长度的过程如图: //#define LOCAL #include <iostream> #include <cstdio> #include <cstring…
Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18387    Accepted Submission(s): 7769 Problem Description A subsequence of a given sequence is the given sequence with some el…
经典的最长公共子序列问题. 状态转移方程为 : 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…
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 not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germa…
题目大意:求两个字符串的最长公共子序列 题目思路: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…
题目链接:https://vjudge.net/contest/124428#problem/A 题目大意:给出两个字符串,求其最长公共子序列的长度. 最长公共子序列算法详解:https://blog.csdn.net/hrn1216/article/details/51534607     (其中的图解很详细)   根据图解理解下面代码 #include<cstdio> #include <string> #include<cstring> #include<i…
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…
题意:给定两个字符串,让你找出它们之间最长公共子序列(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)}…