Dynamic planning maximum substring and

Source: Internet
Author: User

Question G: "Dynamic planning" maximum substring and time limit: 1 Sec memory limit: MB
Submit: $ 22 Solution:
Submitted State [Discussion Version] Title Description

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
Problem-Solving ideas: Reference http://www.cnblogs.com/TWS-YIFEI/p/5590532.html
Code:
#include <iostream>#include<cstdio>using namespacestd;inta[1000005];intsum[1000005];intMain () {intN; Long intN; Long intans=0; scanf ("%d",&N);  for(intz=0; z<n;z++) {scanf ("%ld",&N);  for(Long intI=0; i<n;i++) {scanf ("%d",&A[i]); } ans=0; sum[0]=a[0];  for(Long intI=1; i<n;i++){            if(sum[i-1]>0) {Sum[i]=sum[i-1]+A[i]; }Else{Sum[i]=A[i]; }            if(ans<Sum[i]) {ans=Sum[i]; }        }        if(z!=n-1) {printf ("%d\n", ans); }Else{printf ("%d", ans); }    }    return 0;}

Dynamic planning maximum substring 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.