<题目链接> 题目大意:给你一段序列,要求你求出该序列的最长交替子序列,所谓最长交替子序列就是,这段序列的相邻三项必须是先递增再递减或者先递减再递增这样交替下去. 解题分析: 这与一道dp的典型题求最长上升子序列有点相似,不同的是本题是需要子序列相邻两项需要交替变换,所以在原来的基础上做一些改动,用两个dp数组,分别记录起始状态是递增和起始状态是递减的情况,然后就是根据dp的奇偶性来判断这一步是递增还是递减. #include <cstdio> #include <cstri…
题目链接:https://codeforces.com/contest/1257/problem/E 题意:给三个序列k1,k2,k3,每个序列有一堆数,k1是前缀,k3是后缀,k2是中间,现可以从任意序列中选择一个数加入到另外的序列中,问最小操作次数还原成1-n的原序列(序列内部操作是任意的,不计入操作次数) 题解:k1,k2,k3分别排一下序,然后k1,k2,k3拼成一个序列,求这个序列的最长上升子序列的长度,然后n - lis就是答案. 举个例子k1 : 1 2 6 k2:   3 4 8…
Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 41944   Accepted: 18453 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ...…
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4532    Accepted Submission(s): 1547 Problem Description A palindrome is a symmetri…
题意:最长上升子序列nlogn写法 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ]; ]; int main(){ int n; while(cin>>n){ ;i<n;i++){ cin>>a[i]; } memset(dp,,sizeof(dp)); dp[]=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从…
[BZOJ2423][HAOI2010]最长公共子序列 Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0,y1,…,yk-1”是X的子序列,存在X的一个严格递增下标序列<i0,i1,…,ik-1>,使得对所有的j=0,1,…,k-1,有xij = yj.例如,X=“ABCBDAB”,Y=“BCDB”是X的一个子序列.对给定的两个字符序列,求出他们…
题目链接: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…
最长公共子序列 https://www.nowcoder.com/practice/c996bbb77dd447d681ec6907ccfb488a?tpId=49&&tqId=29348&rp=2&ru=/activity/oj&qru=/ta/2016test/question-ranking 题目描述 对于两个字符串,请设计一个高效算法,求他们的最长公共子序列的长度,这里的最长公共子序列定义为有两个序列U1,U2,U3...Un和V1,V2,V3...Vn,其…
class Solution: def LCS(self,A,B): if not A or not B: #边界处理 return 0 dp = [[0 for _ in range(len(B)+1)]for _ in range(len(A)+1)]#状态定义,dp[i][j]表示当前最长公共的长度 for i in range(1,len(A)+1): #遍历A序列 for j in range(1,len(B)+1): #遍历B序列,时间复杂度为n2 if A[i-1] == B[j-…
题意:给两个长度为\(n\)的全排列,求他们的LCS 题解:这题给的数据范围到\(10^5\),用\(O(n^2)\)的LCS模板过不了,但由于给的是两个全排列,他们所含的元素都是一样的,所以,我们以第一个串为模板,第二个串的每一个元素都能对应到第一个串的元素的位置,第二串对映后的最长上升子序列,就是他们的LCS,也就是我们先离散化一遍,然后求一个LIS\((O(n logn))\)即可. 代码: #include <iostream> #include <cstdio> #inc…
题意:给你n个杠铃的杆子,在给你m个杠铃片,问你能组成多少个重量不同的完整杠铃(杠铃杆子也算一个完整的的杠铃) 解题思路:dfs直接搜,数据很小,每个杠铃片有三种状态(放杆子左边,放杆子右边,两边都不放),用set存一下,去下重,正好要从小到大序输出 #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<set> #define ll…
Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph are playing a two-player game on a triangular board that looks like the following: 1 2 3 4 5 7 8 6 9 10 11 13 14 16 17 12 15 18 At each turn, a playe…
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachments Description Noura Boubou is a Syrian volunteer at ACM ACPC (Arab Collegiate Programming Contest) since 2011. She graduated from Tishreen Un…
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description When selecting files in an application dialog, Vasya noted that he can get the same selection in different ways. A simple mouse click selects a sing…
题意:给出三个序列,求出前两个的公共子序列,且包含第三个序列,要求长度最长. 这道题目怎么做呢,f[i][j]表示a串1-i,b串1-j的最长,g[i][j]表示a串i-n,b串j-m最长, 那么只需要判断中间有没有包好c串就OK了,这样都是O(n^2)的. #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<queue> #include&l…
题目链接: http://codeforces.com/gym/101161/attachments 题意: 总共有n瓶药可供选择 每瓶药可以增加$e_i$点体力,和$p_i$点毒性 每分钟消耗1点毒性,毒性不能大于99,体力不能小于0大于100 击败一只怪物消耗$k$点体力,花费$m$分钟 计算不最大击败怪物的数量 数据范围: $1\leq n\leq 8$ 分析: 定义$dp[i][j][k]$为药物状态为$i$,体力为$j$,毒性为$k$的最大击败数 初始化$dp[0][100][0]=0…
Wavio Sequence  Wavio is a sequence of integers. It has some interesting properties. ·  Wavio is of odd length i.e. L = 2*n + 1. ·  The first (n+1) integers of Wavio sequence makes a strictly increasing sequence. ·  The last (n+1) integers of Wavio s…
Longest common subsequence problem The longest common subsequence (LCS) problem is the problem of finding the longest subsequence common to all sequences in a set of sequences (often just two sequences). It differs from the longest common substring p…
Problem F. Fibonacci SystemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86821#problem/B Description Little John studies numeral systems. After learning all about fixed-base systems, he became inte…
#include<iostream> using namespace std; #define INF 0x7fffffff #define N 10000 // O(n^2) int len[N]; int dp(int *a, int n){ , mxlen = ; ; i < n; ++i) len[i] = ; ; i < n; ++i){ mx = ; ; j < i; ++j) // 寻找len[0...i-1]最大值 if (a[j]<a[i] &…
题目链接:http://ac.jobdu.com/problem.php?pid=1042 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: // // 1042 Coincidence.cpp // Jobdu // // Created by PengFei_Zheng on 24/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include…
F Fighting for Triangles Description Andy and Ralph are playing a two-player game on a triangular board that looks like the following: 1 234 5 7 86 910 11 13 14 16 1712 15 18 At each turn, a player must choose two adjacent vertices and draw a line se…
首先$LIS$显然:$f[i][j]=max(f[i][j-1],f[i-1][j],(a[i]==b[j])*f[i-1][j-1])$ 考虑如何转移数量: 首先,不管$a[i]$是否等于$b[j]$, 都有$h[i][j]+=h[i-1][j]*(f[i][j]==f[i-1][j])+h[i][j-1]*(f[i][j]==f[i][j-1])$ 然后讨论$LIS$中第三种转移: 如果$a[i]==b[j]\ \&\&\ f[i][j]==f[i-1][j-1]+1$,有$h[i][…
Longest Increasing Subsequence The longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. Th…
#include <iostream> #include <string> #include <string.h> #include <vector> #include <time.h> using namespace std; char a[],b[]; int dp[][]; int maxn(int x,int y) { if(x>=y) return x; return y; } int main() { int lena,lenb…
[BZOJ2423][HAOI2010]最长公共子序列 试题描述 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0,y1,…,yk-1”是X的子序列,存在X的一个严格递增下标序列<i0,i1,…,ik-1>,使得对所有的j=0,1,…,k-1,有xij = yj.例如,X=“ABCBDAB”,Y=“BCDB”是X的一个子序列.对给定的两个字符序列,求出他们最长的公共子序…
LIS最长上升子序列 dp[i]保存的是当前到下标为止的最长上升子序列的长度. 模板代码: int dp[MAX_N], a[MAX_N], n; int ans = 0; // 保存最大值 for (int i = 1; i <= n; ++i) { dp[i] = 1; for (int j = 1; j < i; ++j) { if (a[j] < a[i]) { dp[i] = max(dp[i], dp[j] + 1); } } ans = max(ans, dp[i]); }…
         在字符串S中按照其先后顺序依次取出若干个字符,并讲它们排列成一个新的字符串,这个字符串就被称为原字符串的子串          有两个字符串S1和S2,求一个最长公共子串,即求字符串S3,它同时为S1和S2的子串,且要求它的长度最长,就是这里的 最长公共子序列问题.           最长公共子序列问题的递推条件如下:dp[i][j]表示s1前i个字符组成的前缀子串与s2前j个字符组成的前缀子串的最长公共子序列           dp[0][ j ]( 0<=j<=m)…
最长公共子序列LCS Lintcode 77. 最长公共子序列 LCS问题是求两个字符串的最长公共子序列 \[ dp[i][j] = \left\{\begin{matrix} & max(dp[i-1][j], dp[i][j-1]), s[i] != s[j]\\ & dp[i-1][j-1] + 1, s[i] == s[j] \end{matrix}\right. \] 许多问题可以变形为LCS问题以求解 class Solution { public: /** * @param…