• 杭电1003 最大子串(第二次AC) 当作DP的训练吧


    Max Sum

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 252815    Accepted Submission(s): 59950


    Problem Description
    Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
     
    Input
    The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
     
    Output
    For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
     
    Sample Input
    2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
     
    Sample Output
    Case 1: 14 1 4 Case 2: 7 1 6
     
    题目意思很简单,就是在一个数组中找最大子串(这个是要连续的,之前那个是不连续的要求递增的)。
    然后题目的精髓就在于: 一开始把first指针指向第一个位置,然后用一个temp变量也指向第一个位置,然后当有sum>maxsum的时候,就执行first=temp,保证了最大子串在后面依然可以被找到。计算子串的sum<0时就表示要重新找子串了,temp=j+1,sum=0,当找到下一个>0的数的时候,(因为有temp的实时更新)first指向该数,重新开始计算sum。
     
    然后 就要注意下输出格式的问题。具体参考下第一次的AC代码
  • 相关阅读:
    flex布局
    redis持久化的四种方式
    list all index in elasticsearch
    Java Thread停止关闭
    关于线程的一些操作方法
    将redis key打印到文本
    spout和bolt
    java读取redis的timeout异常
    storm中,ack与fail
    好文要收藏(大数据)
  • 原文地址:https://www.cnblogs.com/William-xh/p/7300035.html
Copyright © 2020-2023  润新知