Nyoj 44 sub-strings and

Source: Internet
Author: User

Substring and time limit:MS | Memory limit:65535 KB Difficulty:3
Describe
given an integer sequence {A1,a2...,an}, find the continuous non-empty string {ax,ax+1,..., ay}, making the subsequence and the largest, wherein, 1<=x<=y<=n.
Input
The
first line is an integer N (n<=10) that represents the number of groups of test data)
The first line of each set of test data is an integer n indicating that the sequence has n integers, followed by n integers I ( -100=<i<=100), representing all the elements in the series. (0<n<=1000000)
Output
for each set of test data output and the maximum contiguous substring of the and.
Sample input
151 2-1 3-2
Sample output
5
Tips
a lot of input data, it is recommended to use scanf for input

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;}

  

Nyoj 44 sub-strings and

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.