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