Maximum sub-sequences and problems

Source: Internet
Author: User

maximum sub-sequences and problems : Links: Click here

Problem Description:

Enter a set of integers to find out the set of number sub-sequences and the maximum values. That is, only the maximum number of sub-sequences is required, and the largest sequence is not to be obtained. For example:

Sequence:-2 11-4 13-5-2, the maximum subsequence and 20.

Sequence:-6 2 4-7 5 3 2-1 6-9 10-2, the maximum subsequence and 16.

Several different implementation algorithms are given in the following sequence

int MaxSubseqSum1 (int a[], int N)//Algorithm 1 T (N) = O (N3) {int thissum, maxsum = 0;    int I, j, K; for (i = 0; i < N; i++)/* I is the left-hand position of the child column */{for (j = i; J < N; J + +)/* J is the right side of the child */{Th issum = 0;            /* Thissum is a sub-column from a[i] to a[j] and */for (k = i; k <= J; k++) Thissum + = A[k]; if (Thissum > Maxsum)/* If you just get this sub-column and larger */maxsum = thissum; /* Update results */}/* Loop end */}/* I loop End */return maxsum;}    int MaxSubseqSum2 (int a[], int N)//Algorithm 2T (N) = O (N2) {int thissum, maxsum = 0;    int I, J;  for (i = 0; i < N; i++)/* I is the left-hand position of the child column */{thissum = 0;/* thissum is a child column from a[i] to a[j] and */for (j = i; J < N            J + +)/* J is the right-hand position of the sub-column */{thissum + = A[j]; /* For the same I, different j, just accumulate 1 items on the basis of j-1 cycles */if (Thissum > Maxsum)/* If you just get this sub-column and larger */maxsum = Thissu M /* Update results */}/* Loop end */}/* I loop End */return maxsum;}  int MaxsuBSEQSUM4 (int a[], int N)//Algorithm 4T (N) = O (N2) {int thissum, maxsum;    int i;    thissum = maxsum = 0; for (i = 0; i < N; i++) {thissum + = A[i];/* Add right */if (Thissum > maxsum) maxsum = Th Issum; /* Find larger and update current results */else if (Thissum < 0)/* If current sub columns is negative */thissum = 0; /* It is not possible to make the back part and increase, discard the */} return maxsum;} "On-line" means that each input data is processed in real time, and the input is aborted at any place, and the algorithm can give the current solution correctly.

Algorithm 3---Divide-and-conquer method



Maximum sub-sequences and problems

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.