Source: Rujia "algorithmic Competition Primer-Training Guide" P60 question 6:
problem Description: given n integers a1,a2,..., An, select as many integers as possible from left to right, forming an ascending sub-sequence (the subsequence can be interpreted as: delete 0 or more numbers, and the other order is unchanged). For example, from the sequence 1,6,2,3,7,5, you can select ascending subsequence 1,2,3,5, or you can select 1,6,7, but the former is longer. The selected neighboring elements cannot be equal.
O (n^2) Analysis of time complexity: set D[i] for the length of the longest ascending subsequence ending with I, then d[i]=max{0,d[j] (satisfying j<i,aj<a[i])}+1. The final answer is Max{d[i]}.
(if the adjacent elements in the LIS can be equal, change "<" to "<=").
O (Nlog (n)) time complexity thinking Analysis: Assuming that the two computed states I and J satisfy Ai<aj && D[i]==d[j], then only the AI state can be saved (for subsequent state K, if Ak>ai, The AK must be greater than AJ, and vice versa). We can use G[i] to indicate the current state (starting with the first number) under the sub-sequence length is the minimum number of I, then each consideration of the subsequent number of joins, update g[] can (binary search). (Tips: This idea is only suitable for the maximum length problem)
example Source:http://acm.nyist.net/JudgeOnline/problem.php?pid=17
Example: Nyoj
monotonically increasing the eldest-son sequenceTime limit:MS | Memory limit:65535 KB Difficulty:4
-
Describe
-
The length of the longest increment subsequence of a string
For example: dabdbf maximum increment subsequence is ABDF, length is 4
-
Input
-
The first line is an integer 0<n<20, which indicates that there are n strings to handle
The next n rows, each line has a string that is no longer than 10000
-
Output
-
The length of the longest increment subsequence of the output string
-
Sample input
-
3aaaababcabklmncdefg
-
Sample output
-
137
O (n^2) Time complexity code:
1#include"stdio.h"2#include"string.h"3 4 #defineN 101005 6 intD[n];7 CharStr[n];8 9 intInline Max (intAintb) {returnA>b?a:b;}Ten One intMain () A { - intT; - inti,j; the intLen; -scanf"%d",&T); - while(t--) - { +scanf"%s", str); -Len =strlen (str); + intAns =0; A for(i=0; i<len; ++i) at { -D[i] =1; - for(j=0; j<i; J + +) - { - if(str[i]>Str[j]) -D[i] = Max (d[i],d[j]+1); in } -ans = ans>d[i]?Ans:d[i]; to } +printf"%d\n", ans); - } the return 0; *}
O (Nlog (n)) time Complexity code:
1#include"stdio.h"2#include"string.h"3 #defineN 100054 #defineINF 0X3FFFFFFF5 6 intNum//Num records the length of the oldest sequence in the current sequence7 intG[n];//G[i] Saves the minimum character of the ascending subsequence of the current sequence, length I8 CharStr[n];9 Ten intEr_fen (intLintRintK//binary Lookup, returns the subscript of the rightmost number of values in the array g[] less than k One { A intmid; - if(K>g[r])returnR//are smaller than K, return R - if(K<=g[l])return 0;//are larger than K, returning 0 the while(L +1!=R) - { -Mid = (l+r)/2; - if(G[mid] <k) +L =mid; - Else +R =mid; A } at returnl; - } - - intMain () - { - intT; in intI,k,len; -scanf"%d",&T); to GetChar (); + while(t--) - { thescanf"%s", str); *Len =strlen (str); $ for(i=0, num=0; i<len; i++)Panax NotoginsengG[i] =INF; -num =1; theg[1] = str[0]; + for(i=1; i<len; i++) A { theK = Er_fen (1, Num, (int) str[i]); + if(g[k+1] >Str[i]) -g[k+1] =Str[i]; $ if(k==num) $num++; - } -printf"%d\n", num); the - }Wuyi return 0; the}
;
04_ Longest ascending subsequence problem (LIS)