- 子串:串中任意个连续的字符组成的子序列称为该串的子串;
- 子序列:序列的一部分项按原有次序排列而得的序列;
# -*- coding=utf-8 -*-
##### 1: 连续子串最大和 #####
def MaxSum(arr):
res, s = arr[0], arr[0]
for x in arr[1:]:
s = max(x, s+x)
res = max(res, s)
return res
##### 2: 连续子串最大乘积 #####
def MaxMulti(arr):
maxA, minA = [arr[0]], [arr[0]]
res = arr[0]
for i in range(1, len(arr)):
maxA.append(max(arr[i], maxA[i-1]*arr[i], minA[i-1]*arr[i]))
minA.append(min(arr[i], maxA[i-1]*arr[i], minA[i-1]*arr[i]))
res = max(res, maxA[-1])
return res
#### 3:最长递增子序列 ####
def LIS(arr):
dp = [1] * len(arr)
for i in range(len(arr)):
temp = 1
for j in range(i-1, -1, -1):
if arr[i] > arr[j]:
temp = max(temp, dp[j]+1)
dp[i] = temp
return max(dp)
#### 4:最长公共子串 ####
def LongSubStr(s