Longest monotonic subsequence Solution

Source: Internet
Author: User

Zookeeper

This blog is part 3 of the algorithm series

Learn more


Repeat questions

It is known that a sequence is composed of random numbers, and its longest monotonic subsequence is obtained.

Requirements:The monotonic is classified into two conditions: strict and not strict, and the longest monotonic subsequence and all sub-sequences that meet the requirements are obtained and output respectively.


Problem Analysis

This question is used to solve subsequence problems with constraints and can be solved through dynamic planning. Because this question is about solving the longest monotonic subsequence, including finding the longest monotonic subsequence and solving all the required sequences, the complexity of the algorithm will be discussed in the following two cases.


Algorithm complexity for solving the longest monotonic subsequence

This topic assumes that the sequence length is N (any size), that is, the sequence S [N]. If the direct search method is used, the position in the longest monotonic subsequence of the corresponding sequence element in the array LP [N] is also defined. The algorithm IDEA is as follows: the sequence starts from S [1] (S [0]) and is initialized). Each increment is used to determine the size of each previous value, if S [I]> S [j] (j And LP [I] <= LP [j] + 1, then LP [I] is updated to LP [j] + 1. This ensures that each element is classified into its own satisfying oldest sequence, but the complexity of this algorithm is O (n ^ 2 ).

Through the second-layer loop, the position of the oldest sequence where the newly added element is located can be determined, and the search policy can be improved to reduce the complexity to O (nlogn ). The basic idea is: Define the array Len [N + 1], 0th elements are idle, j (j> 0) stores the minimum values of the last element in all subsequences with a length of j. This ensures that the current sequence is the longest sequence and maintains the local optimum. When the newly added element S [I] is identified, the binary search method is used to search for the position of the element in the Len array. As Len keeps in ascending order, after searching for the position, replace the element larger than him, and then the final element of the sequence with the minimum length. The search complexity is O (logn ). The complexity is O (nlogn) because of the first-level cyclic O (n ). Using LP [N] And S [N], we can cyclically solve the longest monotonic subsequence with the complexity of O (n ). Therefore, the total complexity is O (nlogn ).


Complexity of solving all longest monotonic subsequences

To solve all the longest monotonic subsequences, the only difference between LP [N] And MaxLen is that the solution outputs all the longest monotonic subsequences. The complexity of solving LP [N] And MaxLen is the same as that of analyzing in 2.1, the complexity of the best method is O (nlogn ). Output all the longest monotonic subsequences (set its complexity to O (T) and the above process is parallel rather than nested, so the total complexity is.

The T solution is very easy. Through analysis, we can find that the first n refers to the cyclic LP [N] to obtain the starting position of the longest vertex sequence, and k refers to the number of all the longest vertex sequences, it is smaller than the number because the starting point of the longest sequence is always less than n.

In summary, the complexity of solving all the longest monotonic subsequences is as follows: k is [0, n]. In extreme cases, it is O (n ^ 2). Here we discuss the idea of emphasizing the finite longest monotonic subsequence in an infinite long sequence.



Algorithm ideas and Implementations

A. defines three arrays, namely, S [N], LP [N], and Len [N + 1], which are the sequence itself, and records the position of the oldest sequence corresponding to the sequence element, minimum value of the last element of the subsequence whose length is I. S [N] is randomly generated, and Len [0] is idle;


B. initialize LP [0] to 1, S [N] loops from I = 1 to the N-1; K is used to record the maximum value of Len, that is, the maximum length of the searched oldest sequence, the initial value is K = 1. If S [I]> Len [K] (Note: if not strict, it is "> = "), that is to say, if the value of the next element is greater than the value of the last element in the longest gender sequence, the S [I] is directly placed at the position of Len [++ K; if S [I] <= Len [K], go to Step c;


C. call the function k = fn_InsertPos () and perform the following operations: Len [k] = S [I]; LP [I] = k; LP [I] Still records the position of S [I], which is conducive to the output of the sequence. fn_InsertPos () adopts the binary search method, but different from the conventional search conditions, the algorithm complexity is O (logn );


D. after the S [I] search, the K value recorded by Len is the length of the longest monotonic subsequence. At this time, you can use the records in the LP [N] array to find the oldest sequence, that is, the LP [N] is traversed from the back to the forward, and the subsequence is recorded with the auxiliary array P [N]. k records the length of the subsequence to be saved, when LP [I] = k and S [I] = "), Save S [I] to P [-- k] Until k = 0. For details, see void fn_OutPutLMS (int Pos). Its algorithm complexity is;


E. to output all the longest monotonic subsequences, You need to determine the position of the end element of all the oldest sequence. This is easy to implement, that is, to define the array C [N], traverse LP [N] and record the position of the element with the value of MaxLen. Then voidfn_OutPutLMS (int Pos) is called at one time. The complexity is.


Program Implementation

/* Operator * longest monotonic subsequence problem * operator * By Gu Jinjin SEU * solves the longest monotonic subsequence. There are two conditions: strict and not strict. * Here we use monotonic increasing as an example * Time: 2012/11/28-29 Weather: rainy */# include
 
  
# Include
  
   
# Include
   
    
Using std: cout; using std: endl; // define # define N 5000 // S [N]-sequence, LP [N]-position of the sequence element in the oldest sequence // Len [N + 1]-minimum value of the element at the end of all monotonic subsequences of I length int S [N], LP [N], Len [N + 1]; // records the longest monotonic subsequence length int MaxLen; // function declaration void fn_RandNum (); int fn_InsertPos (int Si, int K); int fn_GetLMS_Len (); void fn_OutPutInitList (); void fn_OutPutLMS (int Pos); void fn_GetAllLMSes ();/* handle * void main (void) * functions * main function */void main () {clock_t t_start, t_end; fn_RandNum (); t_start = clock (); MaxLen = fn_GetLMS_Len (); t_end = clock (); cout <"inrule List:" <
    
     
High, it indicates that the while (low <= high) {if (low> high) break; else if (Len [mid]
     
      
Len [lmn]) {Len [++ lmn] = S [I]; LP [I] = lmn;} else {k = fn_InsertPos (S [I], lmn ); len [k] = S [I]; LP [I] = k;} return (lmn);}/* handle * void fn_OutPutInitSeq (...) * ----------------------------------------------------------------- * output original series */void fn_OutPutInitList () {cout <"S" <'\ t' <"LP" <' \ t' <"Len" <
      
        = 0; I --) {if (LP [I] = k & S [I] <P [k]) P [-- k] = S [I];} // OutPut LMSif (k = 0) {for (int I = 0; I
       
         = MaxLen-1; I --) {if (LP [I] = MaxLen) {C [k] = I; k ++ ;}for (int I = 0; I
        
         

Result (when N is 20, the array is generated by the random function)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.