def incSeq(seq): start = 0 for i in xrange(1, len(seq)): if seq[i] < seq[i-1]: yield start, i - start start = i maxIncSeq = reduce(lambda x,y: x if x[1]>y[1] else y, incSeq(seq)) 得到最长递增子串长度及起始位置,时间复杂度O(n).…
例子 "abmadsefadd"  最长长度为7 "avoaid"           最长长度为3 思路 空间换时间hashTable,起始位置设为beg.初始化全局最大值0.开辟字符数组,起初标为0. 访问数组时 如果该字符在hashTable对应的哈希值为1,则计算当前位置到beg的距离,并且把beg赋值为beg+1.如果大于全局最大值,则替换全局最大值 如果该字符在hashTable对应的哈希值为0,则置1 参考代码 #include <iostrea…
题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ...,…
Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 23696   Accepted: 9705 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days:…
一.最长公共子序列 经典的动态规划问题,大概的陈述如下: 给定两个序列a1,a2,a3,a4,a5,a6......和b1,b2,b3,b4,b5,b6.......,要求这样的序列使得c同时是这两个序列中的部分(不要求连续),这个就叫做公共子序列,然后最长公共子序列自然就是所有的子序列中最长的啦. 既然是动态规划,难点肯定是在转移方程那了.首先我们用一张网上流传的图: 我个人觉得这张图最好的阐述了这个问题的解法.下面说一下我的理解:首先我们要考虑怎么表示LCS中的各个状态,这个知道的可能觉得很…
出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的最长公共子串方法.最长公共子串用动态规划可实现O(n^2)的时间复杂度,O(n^2)的空间复杂度:还可以进一步优化,用后缀数组的方法优化成线性时间O(nlogn):空间也可以用其他方法优化成线性.3.LIS(最长递增序列)DP方法可实现O(n^2)的时间复杂度,进一步优化最佳可达到O(nlogn)…
问题描述:求括号字符串中最长合法子串长度.例如:()((),返回2,而不是4. 算法分析:还是利用栈,和判断合法括号对是一样的. public static int longestValidParentheses(String s) { Stack<int[]> stack = new Stack<int[]>(); int result = 0; for(int i=0; i<=s.length()-1; i++) { char c = s.charAt(i); if(c=…
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than…
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be more…
代码+题解: 1 //题意: 2 //输出在区间[li,ri]中有多少个数是满足这个要求的:这个数的最长递增序列长度等于k 3 //注意是最长序列,可不是子串.子序列是不用紧挨着的 4 // 5 //题解: 6 //很明显前面最长递增序列的长度会影响到后面判断,而且还要注意我们要采用哪种求最长递增序列的方式(一共有两种, 7 //一种复杂度为nlog(n),另一种是n^2),这里我才采用的是nlog(n)的. 8 // 9 //算法思想: 10 //定义d[k]: 11 //长度为k的上升子序列…