首页 > TAG信息列表 > Subsequence

线段树优化最长上升子序列问题

最长上升子序列 给定一个长度为 $N$ 的数列,求数值严格单调递增的子序列的长度最长是多少。 输入格式 第一行包含整数 $N$。 第二行包含 $N$ 个整数,表示完整序列。 输出格式 输出一个整数,表示最大长度。 数据范围 $1 \leq N \leq 1000$,${−10}^{9} \leq \text{数列中的数} \leq {10

[Google] LeetCode 792 Number of Matching Subsequences

Given a string s and an array of strings words, return the number of words[i] that is a subsequence of s. A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative

D2. Xor-Subsequence (hard version)

D2. Xor-Subsequence (hard version) 昨天cf的E题,挺好的一个DP优化问题。 暴力的DP就是设dp[i]表示以i结尾的最长长度。转移时枚举之前的所有j,复杂度O(n^2)。 考虑怎么优化,优化往往都是从转移条件上做文章的,我们考虑当前i的dp值怎么计算, 是所有max(f[j]+1),而且这些j满足\(a_i\)

longest increasing subsequence

300. Longest Increasing Subsequence Medium Given an integer array nums, return the length of the longest strictly increasing subsequence. A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing t

LeetCode 673 Number of Longest Increasing Subsequence

Given an integer array nums, return the number of longest increasing subsequences. Notice that the sequence has to be strictly increasing. Solution 我们需要求出最长长度序列的个数。不妨用两个数组 \(dp1, dp2\). 其中 \(dp1[i]\) 表示以 \(i\) 结尾的最长递增序列的

POJ1458 Common Subsequence

题目链接 题目 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

LeetCode 376 Wiggle Subsequence DP+思维

A wiggle sequence is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two

cf1370 E. Binary Subsequence Rotation

题意: 给定等长的两个01串 \(a,b\),每次操作可选 \(a\) 的一个子列进行 “右移一位” 操作。问把 \(a\) 变成 \(b\) 至少要几次操作 “右移一位” 操作:\(a_1a_2a_3a_4a_5\to a_5a_1a_2a_3a_4\) 思路: 若0的总数和1的总数不同则无解 被操作的子列满足:\(a_i\neq b_i\),且相邻的 \(a_i\)

[LeetCode] 516. Longest Palindromic Subsequence

Given a string s, find the longest palindromic subsequence's length in s. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. Example 1: Input: s

leetcode 300. Longest Increasing Subsequence 最长递增子序列 (中等)

一、题目大意 标签: 动态规划 https://leetcode.cn/problems/longest-increasing-subsequence 给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。 子序列 是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7]

HDU1423 Greatest Common Increasing Subsequence (DP优化)

LIS和LCS的结合。 容易写出方程,复杂度是nm2,但我们可以去掉一层没有必要的枚举,用一个变量val记录前一阶段的最优解,这样优化成nm。 1<=k<j,j增加1,k的上界也增加1,就可以考虑用变量优化去掉一层循环。 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using name

LeetCode 376. Wiggle Subsequence

LeetCode 376. Wiggle Subsequence (摆动序列) 题目 链接 https://leetcode.cn/problems/wiggle-subsequence/ 问题描述 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为 摆动序列 。第一个差(如果存在的话)可能是正数或负数。仅有一个元素或者含两个不等元素的序列

POJ2533 Longest Ordered Subsequence (线性DP)

设dp[i]表示以i结尾的最长上升子序列的长度。 dp[i]=max(dp[i],dp[j]+1). 1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <cstdio> 6 #include <vector> 7 #include <climits> 8 #include &

POJ-3061 Subsequence

Subsequence 找一个最短的区间,使得区间和大于等于s 尺取模板 #include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <string> #include <queue> #include <functional> #include <map> #include <set>

ARC138E Decreasing Subsequence

\(\texttt{或许更好的阅读体验}\) \(\texttt{link}\) 第一步是巧妙的转化:对于 \(a_i>0\),连边 \((i,a_i-1)\)。 对于每个序列 \(a\),连边后形成若干条链。 假设选出的 \(k\) 个点为 \(b_1,b_2,...,b_k\),并且 \(b_1<b_2<...<b_k\),对应的 \(a_{b_1}-1,...,a_{b_k}-1\) 为了方便记作 \(

CF888E Maximum Subsequence 题解

首先看完本题,最直接的想法就是——爆搜! 但是, \(2^{35}\) 让我们望而却步,因此我们需要考虑一定的优化。 而本题的优化是十分经典的 折半搜索 (Meet in middle) 算法。 折半搜索的主要思路就是:将序列裂成两半搜索,然后合并答案。 对于这道题,我们首先对 \([1,\dfrac{n}{2}]\) 做一次暴力

substring和subsequence

substring是子串。例如,‘wee’ 是 ‘helloween’的substring,‘hwn’ 不是 ‘helloween’的substring。取子串的时候不能隔着字符取。 subsequence是子序列。例如,‘wee’ 和 ‘hwn’ 都是 ‘helloween’的subsequence。取子序列的时候可以隔着字符取。 palindromic substring 是回

PAT 甲 1007 Maximum Subsequence Sum

2022.2.10 练习 PAT 甲 1007 Maximum Subsequence Sum (原题链接) #include <bits/stdc++.h> using namespace std; int a[10010]; int dp[10010]; int main() { std::ios::sync_with_stdio(false); int n; cin>>n; int num=0; for(int i=0;i<n;

【POJ】1458 Common Subsequence

最长公共子序列问题是动态规划的经典问题之一,对于长度分别为n和m的两个序列,若是最后一个元素是一样的,那么我们只需要看长度分别为n-1和m-1的两个序列,若是不一样,则需要比较长度分别为n-1和m的两个序列或者长度分别为n和m-1的两个序列哪个的公共子序列是最长的。 递归解法: #in

Wiggle Subsequence

题面 这题,自己写,一点思路都没有!后来看了题解,觉得作者分析得有道理!首先,这个所谓的摆动序列可以分为上升摆动序列,也可分为下降摆动序列。上升摆动序列是最后一个元素相对来说值偏大,使得整个序列呈上升趋势,那么下降摆动序列也就能理解了。上升摆动序列我们可以用up数组表示,up[i]就表

c++PTAA1007(dp累加)

Given a sequence of K integers { N 1 , N 2 , …, N K }. A continuous subsequence is defined to be { N i , N i+1 , …, N j } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, gi

1007 Maximum Subsequence Sum

#include <iostream> #include <vector> #include <cstdio> using namespace std; int main() { int n; cin >> n; vector<int> arr(n); int sum=-1;//最大连续子序列和 int tempsum=0;//临时的连续子序列和 int left=0;//最大子序列左下标

1143. Longest Common Subsequence

找两组数据的最大数值,可以自顶向下写个递归,把可能重复的部分记录下来,用一个二维数组来存储: class Solution { private Integer[][] dp; private int m, n; public int longestCommonSubsequence(String text1, String text2) { m = text1.length();

1007 Maximum Subsequence Sum

 运行结果 思路 1-首先读入输入的数字的个数,int n;cin>>n; 2-接着建立大小为n的数组,读入数据      int s[n];         for(int i=0;i<n;i++){                  cin>>s[i];     }         int sum=-1,ans=0,ll=0,l=0,r=n-1;          for

E-Lexicographically Maximum Subsequence

题意 找一个串的最大字典序字串 题解 正序考虑会遇到无法确定这个数该不该选的情况,遇到的矛盾是无法确定后面有没有比它大的字母,所以我们只需要解决这个问题即可,做题时的基本思路就是正序不想试一试倒序能不能优化,我们可以记录住以i为结尾的最大字符然后第i-1个数与之比较,就