[Programming question] calculates the longest descending subsequence of an array, for example, {9, 4, 3, 2}. The longest descending subsequence is {9, 5, 4, 3, 2}

Source: Internet
Author: User

47. Innovation workshop (algorithm ):
Returns the longest descending subsequence of an array, for example, {9, 4, 3, 2, 5, 3, 2,
4,3, 2}

 

Idea: Dynamic Planning

Calculates the longest descending subsequence of the sequence of the current number. Each time you look for the longest child sequence, scan the child sequence obtained before it, and the first number is smaller than the current number.

For example, the first digit is 2, the maximum length is 1, and the next digit is 7th

The second digit is 3, the maximum length is 2, and the next digit is 7th

The third digit is 4, the maximum length is 3, and the next digit is 6th

The fourth digit is 5, the maximum length is 4, and the next digit is 5th

The fifth digit is 2. the maximum length is 1 and the next digit is 3rd.

The sixth digit is 3, the maximum length is 2, and the next digit is 3rd

The seventh digit is 4, the maximum length is 3, and the next digit is 2nd

The eighth digit is 9, the maximum length is 5, and the next digit is 4th

In this way, you can find the maximum length and find the longest descending sequence based on the next number stored.

/* 47. innovation workshop (algorithm): Find the longest descending sub-sequence of an array, for example, {9, 4, 3, 2, 5, 4, 3}. The longest descending sub-sequence is {9, 5, 4, 3, 2} */# include <stdio. h> # include <stdlib. h> int maxlength (int * a, int Len) {int * Pos = (int *) malloc (2 * Len * sizeof (INT )); // find the longest descent sub-sequence POS starting with each element [2 * (LEN-1)] = 1; // The longest descending sub-sequence length (POS) headed by the last number [2 * len-1] = len-1; // The next digit of the last number for (INT I = len-2; I> = 0; I --) {int maxlen = 0; int nextpos = I; for (Int J = I + 1; j <Len; j ++) {If (a [J] <A [I]) {If (Pos [2 * j]> maxlen) {maxlen = POS [2 * j]; nextpos = J ;}} POS [2 * I] = maxlen + 1; // The longest descending sub-sequence length POS headed by a [I] [2 * I + 1] = nextpos; // next number of a [I]} // find the longest descending sequence int maxstart = 0; int max = 0; For (INT I = 0; I <Len; I ++) {If (Pos [2 * I]> MAX) {max = POS [2 * I]; maxstart = I ;}} // print the longest descending sub-sequence printf ("the longest descending sub-sequence is:"); int next = maxstart; do {printf ("% d", a [next]); next = P OS [2 * next + 1];} while (next! = POS [2 * next + 1]); printf ("% d \ n", a [next]); free (POS); Return Max; // return maximum length} int main () {int A [8] = {9, 4, 3, 2, 5, 4, 3, 2}; int Len = maxlength (A, 8 ); return 0 ;}

 

I found another silly place. In fact, you don't need to store the next number. You just need to find the number with the maximum length less than one and print it.

Http://blog.csdn.net/wumuzi520/article/details/7378306:

#include <iostream>#include <cstring>using namespace std;int Fun(int aIn[],int pTable[],int nLen){    int nMaxLen = 0;    for(int i = nLen-1; i >= 0; --i) {        int nMax = 0;        for(int j = i+1; j < nLen; ++j) {            if(aIn[j] < aIn[i]) {                nMax = nMax < pTable[j] ? pTable[j] : nMax;            }        }        pTable[i] = 1+nMax;        nMaxLen = nMaxLen<pTable[i] ? pTable[i] : nMaxLen;    }    return nMaxLen;}void PrintMaxSubsequence(int aIn[], int pTable[], int nMaxLen, int nLen)   //!!!!!!!!!!{    for(int i = 0,j=0; i < nLen; ++i) {          if(pTable[i] == nMaxLen){            cout << aIn[i] << " ";            nMaxLen--;        }    }    cout << endl;}

 

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.