Another classic question, for a string containing a negative number ARRAY[1...N], to find one of his substrings ARRAY[I...J] (0<=i<=j<=n), so that in all substrings of the array, ARRAY[I...J] and maximum.
Here we need to pay attention to the difference between a substring and a subsequence. A substring is a contiguous number of elements in an exponential group, and the subsequence requires only the order of the elements to be consistent with the array, without a continuous requirement. For an array with an element number n, it contains 2^n subsequence and N (n+1)/2 substrings. If you use the exhaustive method, you need at least O (n^2) time to get the answer. Jay Kadane of Carnegie Mellon University gives a linear time algorithm, and we'll look at how to solve the maximum substring and problem in linear time.
To illustrate the correctness of the Kadane algorithm, two conclusions are needed. First, for ARRAY[1...N], if ARRAY[I...J] is the most satisfying and maximal substring, then for any K (i<=k<=j), we have ARRAY[I...K] and greater than 0. Because if there is K make ARRAY[I...K] and less than 0, then we have ARRAY[K+1...J] and greater than ARRAY[I...J], which we assume the ARRAY[I...J] is the array and the maximum substring contradiction.
Secondly, we can divide the array from left to right into several substrings, so that the sum of the elements of the remaining substrings is less than 0 except for the last substring, and for all substrings ARRAY[I...J] and any K (i<=k<j), there are ARRAY[I...K] and greater than 0. At this point, we are going to show that the maximum substring that satisfies the condition is the prefix of one of these substrings, and it is not possible to span multiple substrings. We assume ARRAY[P...Q], which is an array and a maximum substring, and array[p...q], spanning ARRAY[I...J],ARRAY[J+1...K]. According to our grouping method, there is i<=m<j that makes ARRAY[I...M] and is the maximum value in ARRAY[I...J], there is j+1<=n<k make ARRAY[J+1...N] and is ARRAY[J+1...K] The maximum value. Because ARRAY[M+1...J] makes ARRAY[I...J] and less than 0. At this point we can compare ARRAY[I...M] and ARRAY[J+1...N], if ARRAY[I...M] and greater than ARRAY[J+1...N] then array[i...m]>array[p...q], no array[j+ 1...N]>ARRAY[P...Q], no matter who is big, we can find more than array[p...q] and a larger substring, which contradicts our assumptions, so satisfying the conditions of ARRAY[P...Q] cannot span two substrings. For cases that span more substrings, the existence and larger non-spanning substrings can also be demonstrated by the negative values of each substring. This conclusion also applies to single-element and maximum exceptions.
Based on the above conclusion, we get the execution flow of the Kadane algorithm, iterate through the target array from beginning to end, divide the array into sub-strings satisfying the above conditions, get the maximum prefix of each substring and then compare the maximum prefixes of each substring and get the final answer. We take array={−2, 1,−3, 4,−1, 2, 1,−5, 4} as an example to illustrate the steps of the algorithm briefly. Through traversal, the array can be divided into the following 3 substrings (-2), (1,-3), (4,-1,2,1,-5,4), here for (-2) Such a situation, a separate group. The maximum prefix for each substring is -2,1,6, so the maximum substring of the target string is 6.
Above excerpt from: http://blog.csdn.net/joylnwang/article/details/6859677
#include <stdio.h>int main () {int n,m,i,fu,a,flag,sum,suma;scanf ("%d", &n), while (n--) {fu=-111;flag=0;sum= 0;SUMA=0;SCANF ("%d", &m); for (i=0;i<m;i++) {scanf ("%d", &a); if (a<0) fu=fu>a?fu:a; Consider all negative cases, else flag=1;suma+=a; The sum of each substring is summed if (suma>0) sum=sum>suma?sum:suma;else suma=0;} if (flag==1) printf ("%d\n", sum); elseprintf ("%d\n", Fu);} return 0;}