8.2 Data Structure --- string (Find)

& Longest common subsequence longest common substring differences:

Get two longest common substring of a string, the substring required to be contiguous in the original string. And the longest common subsequence does not require continuous.

A, the longest continuous common substring

Title:   find the longest consecutive string of two common sub-strings
Example:  abccade and dgcadde ==> cad

ideas: dynamic programming 

Two cases are considered:

M[i+1][j+1]=0,             s1[i+1] != s2[j+1]

M[i+1][j+1]=M[i][j]+1,  s1[i+1] == s2[j+1]


Time complexity O (M * N)
space complexity O (M * N)

 

code show as below:

getMaxSubStr DEF (S1, S2): 
    len_s1 = len (S1) 
    len_s2 = len (S2) 
    SB = '' 
    maxS record # 0 = longest common substring of length 
    maxI = 0 # record the last character in the longest common substring position 
    M = [([None] * (len_s2 +. 1)) for I in Range (len_s1 +. 1)] 
    I = 0 
    the while I <len_s1 +. 1: 
        M [I] [0] = 0 
        I + =. 1 
    J 0 = 
    the while J <len_s2 +. 1: 
        M [0] [J] = 0 
        J =. 1 + 
    # recursive formula by using a two-dimensional array to fill in the new 
    I =. 1 
    the while I <len_s1 +. 1: 
        J. 1 = 
        the while J <len_s2 . 1 +: 
            IF List (S1) [-I. 1] == List (S2) [-J. 1]:  
                M [I] [J] = M [. 1-I] [J-. 1] +. 1
                IF M [I] [J]> maxS: 
                    maxS = M [I] [J]
                    maxI = i
            else:
                M[i][j] = 0
            j += 1
        i += 1
    i = maxI - maxs
    while i < maxI:
        sb = sb + list(s1)[i]
        i += 1
    return sb

s1 = 'abcdefg'
s2 = 'bdeg'
res = getMaxSubStr(s1,s2)
print(res)

The results are as follows:

 

Second, the longest common subsequence (not necessarily consecutive)

Title: find the longest common subsequence of two strings (non-continuous)

For example: abcbdab and bdcaba == "bcba

Ideas: dynamic programming,

    M[i][j]=0,            i=0,j=0

    M[i][j]=M[i-1][j-1] + 1             i,j>0,xi=yi

    M[i][j]=max{M[i-1][j],M[i][j-1]}   i,j>0,xi!=yi

 

        S[i][j]=1        s1[i] == s2[j]

     S[i][j]=2        s1[i] != s2[j] 且 M[i-1][j] >=M[i][j-1]

     S[i][j]=3        s1[i] != s2[j] 且 M[i-1][j] < M[i][j-1]

 

code show as below:

DEF the LCS (S1, s2): 
    # S1 row, s2 columns 
    len_s1 = len (S1) 
    len_s2 = len (S2) 
    SB = '' 
    M = [([None] * (len_s2 +. 1)) for I in Range (len_s1 + 1'd)] 
    S = [([None] * (+ len_s2. 1)) for I in Range (+ len_s1. 1)] 
    I = 0 
    the while I <len_s1 +. 1: 
        M [I] [0] = 0 
        S [I ] [0] = 0 
        I + =. 1 
    J = 0 
    the while J <len_s2 +. 1: 
        M [0] [J] = 0 
        S [0] [J] = 0 
        J + =. 1 
    # by using a recursive formula to fill the newly created two-dimensional array 
    I =. 1 
    the while I <len_s1 +. 1: 
        J. 1 = 
        the while J <len_s2 +. 1: 
            IF S1 [-I. 1] == S2 [-J. 1]:
                M[i][j] = M[i-1][j-1] + 1
                S[i][j] = 1
            elif M[i-1][j] >= M[i][j-1]:
                M[i][j] = M[i-1][j]
                S[i][j] = 2
            else:
                M[i][j] = M[i][j-1]
                S[i][j] = 3
            j += 1
        i += 1
    # print(M)
    return M[-1][-1],S

def cLCS(i,j,S,s1):
    if i == 0 or j == 0:
        return
    if S[i][j] == 1:
        cLCS(i-1,j-1,S,s1)
        print (s1[i - 1], end='')
    elif S[i][j] == 2:
        cLCS(i-1,j,S,s1)
    else:
        cLCS(i,j-1,S,s1)

s1 = "abcbdab ' 
s 2 =' bdcaba ' 
max, S = LCS (s1, s2) 
print (S) 
# print (referred to as (S), only the (S [0])) 
CLCs (referred to as (S) -1, only the (S [0]) - 1, S, s1) 
# print (max)

 

The results are as follows:

 

Third, find the longest string in the palindromic substring

Title: Given a string  s, to find  s the longest substring palindromic. You may assume that  s the maximum length of 1000.

Example: 'CDCA' palindromic longest string 'cdc'

Thinking: each element traversing the string, then expanded around the center point to the element, taking the maximum length

 

code show as below:

Solution class (): 
    DEF the __init __ (Self): 
        self.max_len = 0 
        self.res = '' 

    DEF getLongestPalindrome (Self, S): 
        IF len (S). 1 ==: 
            return 
        Start = 0 
        for I in Range (. 1, len (S)): 
            TMP1 self.max_side = (S, I, I) # extended to the center point of the number of 
            IF TMP1> self.max_len: 
                self.max_len TMP1 = 
                Start = I - 2 // TMP1 

            TMP2 = Self extended .max_side (s, i-1, i) # from this number and the number in front of the two atoms to a center point = 
            IF TMP2> self.max_len: 
                self.max_len TMP2 = 
                Start = I - 2 TMP2 // 
        Self .res = s [start: start + self.max_len]
        S return [Start: Start + self.max_len] 

    DEF max_side (Self, S, I, J): 
        maxS = 0 
        IF J == I: # is a number of singular central 
            maxS. 1 = 
            I -. 1 = 
            J + = . 1 

        the while I> 0 and J = <len (S) and S [I] == S [J]: # doubling in the same two characters as the center 
            maxS 2 + = 
            I -. 1 = 
            J + =. 1 
        return maxS 

    #leetcode fastest code 
    DEF longestPalindrome_best (Self, S): 
        "" " 
        : type S: STR 
        : rtype: STR 
        " "" 
        length = len (S) 
        IF length <== S 2 or S [:: -. 1 ]: return S 
        max_len, the begin =. 1, 0
        for i in range(1, length):
            odd = s[i - max_len - 1:i + 1]
            even = s[i - max_len:i + 1]
            if i - max_len >= 1 and odd == odd[::-1]:
                begin = i - max_len - 1
                max_len += 2
                continue
            if i - max_len >= 0 and even == even[::-1]:
                begin = i - max_len
                max_len += 1
        return s[begin:begin + max_len]

S = Solution()
res = S.longestPalindrome_best(s='abaad')
print(res)

The results are as follows: aba

 

Four, and the length of the longest string of consecutive subframes 0

Title: a one-dimensional array, only 1 and -1, implementor, summing the longest substring of length 0, and the time and space complexity is given in the notes

Ideas: In i from 0 to n, calculate the sum (i), sum (i ) represents the element and from 0 to i. Dic and stored in the dictionary, value is an index i, each receive a sum (i) in the subsequent traversal of the keys to see whether there dic this sum (i) the value, if there is subtracted from the current position i save the i, and compared with maxLen, take the big one. Traversing the end, given the results. Time complexity of O (n), the spatial complexity is O (1)

code show as below:

MIN_LEN DEF (L): 
    DIC = {0: -1} 
    SUM = 0 
    maxLen = 0 
    for X in Range (0, len (L)): 
        SUM = L + [X] 
        Print (DIC) 
        IF SUM in DIC: # If there is the same number of occurrences, indicating the number of numbers between two and the second number is equal 0 
            maxLen = max (maxLen, X - DIC [sUM]) 
        the else: 
            X DIC [sUM] = 
    return maxLen 

Print (MIN_LEN ([3,5, -1, -6,2]))

 

[] Extended and continuous for the longest substring given value

Ideas: to traverse, and to find the sub-string of s, the maximum length of stay

code show as below:

def findarr(s,nums):
    if not nums:
        return 0
    res = -2 ** 31
    for i in range(4,len(nums)):
        pos = i + 1
        while pos < len(nums)-2 and sum(nums[i:pos+1]) < s:
            pos += 1
        if sum(nums[i:pos+2]) == s and pos - i + 1 > res:
            print(i,pos)
            res = pos - i + 1
    print(res)

s = 7
nums = [2,3,0,2,4,2,0,0,1,2,0,0,2,2]
findarr(s,nums)

 

  

 

Five, and greater than or equal to a given value of the shortest continuous substring

Title: Given a positive integer with n a positive integer and an array of s, which satisfies identify the array and the minimum length of a continuous ≥ s subarray. If successive sub-array qualifying does not exist, 0 is returned.

Example: Input: s = 7, nums = [ 2,3,1,2,4,3] Output: 2

Explanation:  subarray [4,3] is the smallest length of a contiguous subarray under this condition.

Thinking 1: traversing each, and to find a length greater than or equal to a given value, and then, traversing backwards, until all positions traversed.

2 ideas: sliding window, from left to right is greater than the number s added, and then deleted from the left, then delete also obtained if the number is greater than s, the current length of the recording, if not, continue right, addend

 

Thinking 1 code is as follows:

def findarr(s,nums):
    # nums.sort() #[4,3,3,2,2,1]
    if not nums:
        return 0
    res = 2 ** 31
    for i in range(len(nums)):
        pos = i + 1
        while pos < len(nums)-2 and sum(nums[i:pos+1]) < s:
            pos += 1
        if pos - i + 1 < res:
            print(i,pos)
            res = pos - i + 1
    print(res)

  

 Thinking 2 code is as follows:

def minSubArrayLen2(s, nums):
    cur_sum = 0
    n = len (nums)
    res = float ("inf")
    l = 0
    for i in range (n):
        cur_sum += nums[i]
        while cur_sum >= s:
            res = min (res, i - l + 1)
            cur_sum -= nums[l]
            l += 1
    return res if res != float ("inf") else 0

s = 7
nums = [2,3,1,2,4,3]
res = minSubArrayLen2(s,nums)
print(res)

Results: res = 2  

 

Six consecutive sub-sequence and the maximum

 Title: Given the nums an array of integers, and find a maximum of the successive sub-array (sub-array comprising a least one element), and has returned to its maximum.

Example:  Input: [-2,1, -3,4, -1,2,1, -5,4], output: 6

Explanation:  Continuous subarray [4, -1,2,1], and the maximum was 6.

Advanced:  If you have already achieved a complexity of O (n) of the solution, try to solve a more sophisticated use of divide and conquer. O (n)

 

Ideas 1: Always keep the maximum value, and if the current is smaller than n, and the current will take n; otherwise, plus the number and then use c_res record the maximum subsequence

code show as below:

maxSubArray1 DEF (the nums): 
    S, TS = - 31 is 2 **, - 2 ** 31 is 
    res_ = [] 
    C_RES = [] 
    for n-in the nums: 
        IF n-> TS + n-: # and if the current is smaller than n, and to take the current maximum n- 
            TS = n- 
            res_ = [n-] 
        the else: # otherwise, taking TS + n- 
            TS = TS + n- 
            res_.append (n-) 
        IF S <TS: 
            S = TS 
            C_RES = List (tuple (res_) ) 
            Print ( "C_RES =% S, S% = RES _"% (C_RES, RES _)) # C_RES sequence record the maximum 
    return S 

# maxSubArray1 RES = ([. 1, -2]) 
# Print (RES)

  

Thought 2: If the array into left and right two, then the sum of the maximum consecutive sub-sequences, or appear in the left half of the array, or appear in the right half of the array, or in the middle, i.e. from the left and right half of the adjacent section of the local district. So you can use divide and conquer to solve, it needs specific implementation recursively

code show as below:

import math
def CalMax(a, b, c):#三个数比较大小
    if a > b:
        if a > c:
            return a
        else:
            return c
    else:
        if b > c:
            return b
        else:
            return c


MaxLeftSum = 0
MaxRightSum = 0
number = [7, 0, 6, -1, 1, -6, 7, -5]


def MaxCalculator(left, right):
    middle = int(math.modf((left + right) / 2)[1])
    if left == right:
        if number[left] > 0:
            return number[left]
        else:
            return 0

    MaxLeftSum = MaxCalculator(left, middle)
    MaxRightSum = MaxCalculator(middle + 1, right)
    MLASum = 0
    MRASum = 0
    MSum = 0

    i = middle
    while i >= left:
        MSum += number[i]
        if MSum > MLASum:
            MLASum = MSum
        i = i - 1
    MSum = 0

    i = middle + 1
    while i <= right:
        MSum += number[i]
        if MSum > MRASum:
            MRASum = MSum
        i = i + 1
    return CalMax(MaxLeftSum, MaxRightSum, MLASum + MRASum)

n=6
result = MaxCalculator(0,n-1)
print(result)

Results: 13

 

Guess you like

Origin www.cnblogs.com/nxf-rabbit75/p/11815072.html