zoukankan      html  css  js  c++  java
  • python 求最大子序列

     动态规划的本质,是对问题状态的定义状态转移方程的定义
    dynamic programming is a method for solving a complex problem by breaking it down into a collection of simpler subproblems.

    动态规划是通过拆分问题,定义问题状态和状态之间的关系,使得问题能够以递推(或者说分治)的方式去解决。

     1 #coding = utf-8
     2 
     3 '''
     4 此题就是一个动态规划题,
     5 在到每一个位置的时候,标记一个局部最大L值,代表以当前位置为结尾的最大子串,
     6 当我遍历到第i个,那么以第i个为结尾的最大子串就是我们要求的L。
     7 而最终要求的全局最大值记为M,它肯定出自局部最大值L。
     8 解题思路:
     9 以位置1为结尾的最大子串;
    10 以位置2为结尾的最大子串;
    11 依次类推
    12 '''
    13 import sys
    14 def MaxString(nums):
    15     L=M=-float('inf')
    16     for n in nums:
    17         L = max(n, L + n)
    18         M = max(L, M)
    19     return M
    20 
    21 if __name__=='__main__':
    22     a=[int(i) for i in sys.stdin.readline().split(' ')]
    23     print(MaxString(a))
  • 相关阅读:
    左偏树
    output html
    jsp web.xml
    mysql link db
    beamline
    jsp embend java into html / mix scriptlets and HTML
    StringTokenizer
    TreeSet
    centOS 显示中文
    request and response
  • 原文地址:https://www.cnblogs.com/smuxiaolei/p/7435634.html
Copyright © 2011-2022 走看看