传送门 DP 十分显然的DP,但是不好写 设 f[ i ] 表示以第 i 个数作结尾时的方案数,原序列为 a 如果不考虑相同的序列: 那么转移就是 Σ f[ j ] (0< j < i && a [ j ] < a [ i ]) 复杂度为 O(n^2) 考虑优化: 先去重 ,得到数组 b 每次把f [ i ] 加到树状数组里 a [ i ]的值 在 b 中的位置 的位置 那么 f [ i ] 就等于 query(a [ i ] 的值在 b 中的位置-1) (query为树…
A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequences: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The follo…
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100). A subsequence of…
A sequence of numbers is called a wiggle sequence if 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 fewer than two…
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return…
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative…
public class Solution { /** * @param A, B: Two string. * @return: the length of the longest common substring. */ public int longestCommonSubsequence(String A, String B) { int lenA = A.length(); int lenB = B.length(); if(lenA==0 || lenB==0){ return 0;…
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Have you met this question in a real interview? Example For [5, 4, 1, 2, 3], the LIS is [1, 2, 3], return 3 For [4, 2, 4,…
先要搞明白:最长公共子串和最长公共子序列的区别. 最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,LCS):不必连续 实在是汗颜,网上做一道题半天没进展: 给定一个字符串s,你可以从中删除一些字符,使得剩下的串是一个回文串.如何删除才能使得回文串最长呢?输出需要删除的字符个数. 首先是自己大致上能明白应该用动态规划的思想否则算法复杂度必然过大.可是对于回文串很难找到其状态和状态转移方程,换句话…
原题:http://www.lydsy.com/JudgeOnline/problem.php?id=3173 题解:促使我写这题的动力是,为什么百度遍地是Treap,黑人问号??? 这题可以用线段树做.我们知道,插入一个数只会使答案变大1或不变.用线段树维护长度为i的最长上升子序列末尾的位置.每插入一个数,可以在线段树中找出插入位置,然后更新即可. #include <bits/stdc++.h> #define N 100006 using namespace std; int n,m,x…