Source: Internet
Author: User

Maximum subsequence and problems, subsequence Problems

**Maximum subsequences and Problems**: Link: click here

**Problem description:**

Enter a group of Integers to obtain the subsequence of the numbers and the medium and maximum values. That is, as long as the sum of the largest subsequences is obtained, the largest sequence does not need to be obtained. For example:

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

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

The following describes several different implementation algorithms in 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 end position of the Child column */{for (j = I; j <N; j ++) /* j is the right position of the Child column */{ThisSum = 0;/* ThisSum is the Child column from A [I] to A [j] And */for (k = I; k <= j; k ++) ThisSum + = A [k]; if (ThisSum> MaxSum)/* if the obtained sub-column is larger */MaxSum = ThisSum; /* then the update result */}/* j loop ends */}/* I loop ends */return MaxSum;} int MaxSubseqSum2 (int A [], Int N) // algorithm 2 T (N) = O (N2) {int ThisSum, MaxSum = 0; int I, j; for (I = 0; I <N; I ++)/* I is the left position of the Child column */{ThisSum = 0; /* ThisSum is the subcolumn from A [I] to A [j] And */for (j = I; j <N; j ++) /* j is the right position of the Child column */{ThisSum + = A [j];/* for the same I, different j, you only need to add one item on the basis of the J-1 loop */if (ThisSum> MaxSum)/* if the child column you just got is larger */MaxSum = ThisSum; /* Then update the result */}/* j loop end */}/* I loop end */return MaxSum;} int MaxSubseqSum4 (int A [], int N) // algorithm 4 T (N) = O (N2) {int ThisSum, MaxSum; int I; ThisSum = MaxSum = 0; for (I = 0; I <N; I ++) {ThisSum + = A [I];/* accumulate to the right */if (ThisSum> MaxSum) MaxSum = ThisSum; /* if larger, update the current result */else if (ThisSum <0)/* if the current Child column is negative */ThisSum = 0; /* cannot increase the following parts. */} return MaxSum;} // "online" means that each input data is processed instantly, the algorithm can correctly provide the current solution when the input is aborted anywhere.

Algorithm 3 --- divide and conquer

Related Article