Lis longest incrementing subsequence Problem

Source: Internet
Author: User

1. Description of the longest incrementing subsequence

Set L = <A1, A2 ,..., An> is a sequence of n different real numbers. The ascending subsequence of L is such a subsequence lin = <ak1, ak2 ,..., AKM>, where k1 <k2 <... <Km and ak1 <ak2 <... <AKM. Evaluate the maximum m value.

For example, the longest descending subsequence of int * indium = {9, 4, 3, 2, 5, 4, 3} is {9, 5, 4, 3, 2 };

 

2. solution:

1. Use a temporary array TMP to save such a State: TMP [I] indicates the length of the incremental sequence ending with I;

For example, if the value of TMP is {3, 2, 5}, TMP = {1, 1, 2}, TMP [2] = 2 indicates that it contains I = 2 bits (indium [2] = 5) number of LIS, that is, the number of SEQUENCES {3, 5} is 2;

 

2. If we already know the status of TMP [0] To TMP [n-1], how can we obtain the status of TMP [N] through the status of TMP [0] To TMP [n-1?

For example, we have obtained TMP [0] = 1, TMP [1] = 1, and TMP [1] = 1. How can we find TMP [2]?

If I <2, I may be in the LIS sequence with 2 as the end point, then the length of the incremental sequence with 2 as the end point is at least the longest sequence of I + 1, the following two conditions must be met:

Indium [I] <indium [N] TMP [I] + 1> TMP [N]

For example, when I = 0, indium [0] = 3 <indium [N] = 5 and TMP [0] + 1 = 2> TMP [2] = 1 (TMP [I] initial values are 1 ); the value is TMP [2] = TMP [0] + 1 = 2;

Continue to compare I = 1. In this case, indium [1] = 2 <indium [N] = 5 and TMP [1] + 1 = 2 is not greater than TMP [2] = 2;

Loop until n-1 bits! Finally, TMP = {1, 1, 2} is obtained };

The maximum element of TMP is the maximum length of LIS!

 

3. How do we output all Lis values?

In fact, we only need to know the position of the first element of each element in LIS:

For example, if the maximum value of TMP = {1, 1, 2} is 2, the last element of the LIS appears at I = 2, if we save the element prefixed by "P [2] = 5" in LIS, and so on, we can find all the elements in LIS;

For example, the lis of {3, 2, 5} is {3, 5} or {2, 5}. We use another temporary array to store its first element subscript int arr = {-1 }, it indicates the LIS ending with P [2] = 5, and the small mark of the previous element is 0, that is, P [0] = 3, so that {3, 2, 5} Lis Is {3, 5 };

 

# Include <stdlib. h> # include <stdio. h> # include <string. h> # include <assert. h> // obtain the longest incrementing sub-sequence; // RET [I] stores the number of LIS elements containing the I-bit; // path stores the longest incrementing sub-sequence path; path [I] stores the subscript of the first element of LIS that contains the I-bit; int Lis (int * indium, int Len, int * ret, int * path) {assert (indium); If (LEN <= 0) return; int I = 0, max = 0, maxpoint = 0; For (; I <Len; I ++) {RET [I] = 1; path [I] =-1; // The initial values are-1. For future output convenience, the initial values 0 and path [0] are mixed; int J = 0; For (; j <I; j ++) {If (indium [I]> indium [J] & RET [J] + 1> RET [I]) {RET [I] = RET [J] + 1; path [I] = J;} printf ("RET [I] = % d \ n", RET [I]); If (Ret [I]> MAX) {max = RET [I]; maxpoint = I; // subscript of the largest element in RET;} return maxpoint;} // output array void printindium (int * indium, int Len) {int I = 0; For (; I <Len; I ++) {printf ("Indium = % d \ n", indium [I]);} // output Lis;-1 in LIS: Lis containing this bit element, with no elements in front of it; void printpath (int * indium, int * path, int key) {for (; key> = 0;) {printf ("Indium [% d] = % d \ n", key, indium [Key]); if (Key = 0) break; // The path [0] will have an endless loop and must jump out of key = path [Key] ;}} int main () {int indium [] = {,}; int Len = sizeof (indium)/sizeof (INT); int ret [Len]; int path [Len]; int maxpoint = Lis (indium, Len, RET, PATH); printpath (indium, path, maxpoint );}

Output result:

ret[i] ==1ret[i] ==2ret[i] ==2ret[i] ==3ret[i] ==4ret[i] ==4ret[i] ==1ret[i] ==2ret[i] ==3inp[4]=8inp[3]=6inp[2]=4inp[0]=2

The result of lis is 8 6 4 2. The output is correct!

 

Lis longest incrementing subsequence Problem

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.