Longest monotonic decreasing subsequence

Source: Internet
Author: User

Problem description: Find the longest descending subsequence of an array, for example, {9, 4, 3, 2}. The longest descending subsequence is {9, 5, 4, 3, 2 }.

Idea: This is a standard dynamic planning problem. When you do not understand the algorithm, you are most likely to use the recursive idea. Actually, it is correct. At last, we will provide a recursive method, after knowing that it is a dynamic planning problem, we need to analyze it. We need an auxiliary array to record information. If the source array is SRC and the auxiliary array is table, the table [I] Array records SRC [0] ~ The SRC [I] sub-array contains the length of the longest monotonous sub-sequence consisting of SRC [I] (Note that table [I] records the decrease composed of SRC [I ]. the longest in the sequence, this is the longest descent sub-sequence ending with SRC [I]). If SRC [I] is greater than all the previous elements, then table [I] = 1.

The final result is: Table [I] = max {table [k] + 1, Src [I]> SRC [k] & 0 <k <I }, this is the table array initialized, so the longest descending subsequence is max {table [I], 0 <I <n }.

However, we need to print the oldest sequence. We have recorded the subscript of max {table [I], 0 <I <n}. Then we know the last element of the oldest sequence, at the same time, we know the length of the eldest son sequence. The following is a recursive call. If we already know that table [k] is the largest, Src [k] is the last in the longest descending subsequence, if table [I] + 1 = table [k] & SRC [I] <Table [K], you need to promise SRC [I].

# Include <stdio. h> # include <stdlib. h> void find (int src [], int N, int table []) {table [0] = 1; int I, j, Maxi = 0; for (I = 0; I <n; I ++) {table [I] = 1; for (j = 0; j <I; j ++) {/* this may be a common error. If (SRC [J]> SRC [I]) {table [I] = table [J] + 1; if (SRC [J]> SRC [I]) Table [I] = table [J] + 1; if (Table [I]> table [Maxi]) maxi = I;} */If (SRC [J]> SRC [I] & table [J] + 1> table [I]) {table [I] = table [J] + 1; // This is used to print the existence. The last array element of the maxi record is if (Table [I]> Table [Maxi]) maxi = I ;}}// printf ("% d \ r \ n", Maxi) ;}void print (INT SRC [], int table [], int Maxi) {int I = maxi-1; for (; I> = 0; I --) {If (Table [Maxi] = table [I] + 1 & SRC [I]> SRC [Maxi]) {print (SRC, table, I ); break ;}} printf ("% d", Src [Maxi]);} void findlongestdscarray2 (int * arr, int N) {int mark [9]; int link [9]; int I = 0; for (I = 0; I <n; I ++) {mark [I] = 0; link [I] =-1;} // link [0] = 1; Int J = 0, maxmark = 0; for (I = 0; I <N; I ++) {Maxmark = 0; For (j = 0; j <I; j ++) {If (ARR [J]> arr [I]) {If (maxmark <mark [J]) {maxmark = mark [J]; Link [I] = J ;}} mark [I] = maxmark + 1 ;} for (I = 0; I <9; I ++) printf ("% d", Mark [I]); printf ("\ r \ n "); for (I = 0; I <9; I ++) printf ("% d", link [I]); printf ("\ r \ n "); /* // print () int node = 0; maxmark = 0; for (I = 0; I <n; I ++) {If (MARK [I]> maxmark) node = I;} while (node! =-1) {printf ("% d", arr [node]); node = link [node];} */} int main () {int SRC [] = {9, 4, 3, 2, 5, 4, 3, 2, 4}; int table [9]; find (SRC, 9, table); findlongestdscarray2 (SRC, 9 ); int I = 0; For (; I <9; I ++) printf ("% d", table [I]); printf ("\ r \ n "); return 0 ;}






Longest monotonic decreasing subsequence

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.