zoukankan      html  css  js  c++  java
  • 动态规划_leetcode474

    # coding=utf8

    # 递归
    # 递归过程是错的,求子集过程错误
    class Solution1(object):
    def findMaxForm(self, strs, m, n):
    """
    :type strs: List[str]
    :type m: int
    :type n: int
    :rtype: int
    """


    return self.tryFindMaxForm(strs, 0, m, n)


    def ZeroAndOne(self,str):

    length = len(str)
    zeroCount = 0

    for item in str:
    if item == "0":
    zeroCount += 1

    return zeroCount,length-zeroCount


    def tryFindMaxForm(self,strs,index,m,n):


    # if m < 0 or n < 0:
    # return 0

    res = 0
    for i in range(index,len(strs)):

    zeroNums,oneNums = self.ZeroAndOne(strs[i])

    if m-zeroNums >= 0 and n-oneNums >= 0 :

    res = max(self.tryFindMaxForm(strs,index+1,m,n),1 + self.tryFindMaxForm(strs,index+1,m-zeroNums,n-oneNums))

    return res



    # A1= ["11110", "00011", "11100"]
    # m1 = 2
    # n1 = 2
    #
    # A2 = ["10", "0", "1"]
    # m2 =1
    # n2 = 1
    #
    # A3 = ["10", "0001", "111001", "1", "0"]
    # m3 = 5
    # n3 = 3
    #
    # s = Solution1()


    # print s.findMaxForm(A3,m3,n3)


    # 记忆化递归
    class Solution2(object):
    def findMaxForm(self, strs, m, n):
    """
    :type strs: List[str]
    :type m: int
    :type n: int
    :rtype: int
    """
    length = len(strs)

    self.memo = [[[-1 for i in range(n+1)] for i in range(m+1)] for i in range(length+1)]


    return self.tryFindMaxForm(strs, 0, m, n)


    def ZeroAndOne(self,str):

    length = len(str)
    zeroCount = 0

    for item in str:
    if item == "0":
    zeroCount += 1

    return zeroCount,length-zeroCount


    def tryFindMaxForm(self,strs,index,m,n):

    if self.memo[index][m][n] != -1:
    return self.memo[index][m][n]

    if m < 0 or n < 0:
    return 0

    res = 0
    for i in range(index,len(strs)):

    zeroNums,oneNums = self.ZeroAndOne(strs[i])

    if not (m-zeroNums < 0 or n-oneNums < 0) and index < len(strs):

    res = max(res,1 + self.tryFindMaxForm(strs,index+1,m-zeroNums,n-oneNums))


    self.memo[index][m][n] = res
    return res


    # A1= ["11110", "00011", "11100"]
    # m1 = 2
    # n1 = 2
    #
    # A2 = ["10", "0", "1"]
    # m2 =1
    # n2 = 1
    #
    # A3 = ["10", "0001", "111001", "1", "0"]
    # m3 = 5
    # n3 = 3
    #
    # s = Solution2()
    #
    #
    # print s.findMaxForm(A3,m3,n3)



    # F(i,m,n) = max(F(i+1,m,n), F(i+1,m-a,n-b))

    class Solution3(object):
    def findMaxForm(self, strs, m, n):
    """ :type strs: List[str] :type m: int :type n: int :rtype: int """ return self.tryFindMaxForm(strs, 0, m, n) def ZeroAndOne(self,str): length = len(str) zeroCount = 0 for item in str: if item == "0": zeroCount += 1 return zeroCount,length-zeroCount def tryFindMaxForm(self,strs,index,m,n): if m < 0 or n < 0 or index >= len(strs): return 0 res = 0 zeroNums,oneNums = self.ZeroAndOne(strs[index]) if m-zeroNums >= 0 and n-oneNums >= 0: res = max(self.tryFindMaxForm(strs,index+1,m,n),1 + self.tryFindMaxForm(strs,index+1,m-zeroNums,n-oneNums)) else: res = self.tryFindMaxForm(strs,index+1,m,n) return res# A1= ["11110", "00011", "11100"]# m1 = 2# n1 = 2## A2 = ["10", "0", "1"]# m2 =1# n2 = 1## A3 = ["10", "0001", "111001", "1", "0"]# m3 = 5# n3 = 3## s = Solution3()### print s.findMaxForm(A2,m2,n2)class Solution4(object): def findMaxForm(self, strs, m, n): """ :type strs: List[str] :type m: int :type n: int :rtype: int """ # self.memo = [[-1 for i in range(n + 1)] for i in range(m+1)] for i in range(len(strs))] self.memo = [[[-1 for i in range(n+1)] for i in range(m+1)] for i in range(len(strs) + 1)] self.tryFindMaxForm(strs, 0, m, n) return self.memo[0][m][n] def ZeroAndOne(self,str): length = len(str) zeroCount = 0 for item in str: if item == "0": zeroCount += 1 return zeroCount,length-zeroCount def tryFindMaxForm(self,strs,index,m,n): if self.memo[index][m][n] != -1: return self.memo[index][m][n] if m < 0 or n < 0 or index >= len(strs): return 0 res = 0 zeroNums,oneNums = self.ZeroAndOne(strs[index]) if m-zeroNums >= 0 and n-oneNums >= 0: res = max(self.tryFindMaxForm(strs,index+1,m,n),1 + self.tryFindMaxForm(strs,index+1,m-zeroNums,n-oneNums)) else: res = self.tryFindMaxForm(strs,index+1,m,n) self.memo[index][m][n] = res return res# A1= ["11110", "00011", "11100"]# m1 = 2# n1 = 2## A2 = ["10", "0", "1"]# m2 =1# n2 = 1## A3 = ["10", "0001", "111001", "1", "0"]# m3 = 5# n3 = 3## s = Solution4()### print s.findMaxForm(A2,m2,n2)# 动归class Solution5(object): def findMaxForm(self, strs, m, n): """ :type strs: List[str] :type m: int :type n: int :rtype: int """ # self.memo = [[-1 for i in range(n + 1)] for i in range(m+1)] for i in range(len(strs))] length = len(strs) self.memo = [[[0 for i in range(n+1)] for i in range(m+1)] for i in range(length+1)] # F(i,m,n) = max(F(i+1,m,n),F(i+1,m-a,n-b)) # for i in range(m+1): # for j in range(n+1): # self.memo[length][i][j] = 0 for index in range(length-1,-1 ,-1): for mIndex in range(m+1): for nIndex in range(n+1): zeroNums, oneNums = self.ZeroAndOne(strs[index]) if mIndex - zeroNums >= 0 and nIndex - oneNums >= 0: self.memo[index][mIndex][nIndex]= max(self.memo[index+1][ mIndex][nIndex],1+self.memo[index+1][mIndex-zeroNums][nIndex-oneNums]) else: self.memo[index][mIndex][nIndex] = self.memo[index+1][mIndex][nIndex] return self.memo[0][m][n] def ZeroAndOne(self,str): length = len(str) zeroCount = 0 for item in str: if item == "0": zeroCount += 1 return zeroCount,length-zeroCountA1= ["11110", "00011", "11100"]m1 = 2n1 = 2A2 = ["10", "0", "1"]m2 =1n2 = 1A3 = ["10", "0001", "111001", "1", "0"]m3 = 5n3 = 3s = Solution4()A4 = ["1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101","1101","0101"]m4 = 100n4 = 100print s.findMaxForm(A4,m4,n4)
  • 相关阅读:
    ZR#330. 【18 提高 3】矿石(容斥)
    牛客NOIP提高组R1 C保护(主席树)
    BZOJ1026: [SCOI2009]windy数(数位dp)
    AtCoderBeginnerContest109题解
    BZOJ3679: 数字之积(数位dp)
    牛客NOIP普及组R1 C括号(dp)
    牛客NOIP提高组R1 A中位数(二分)
    BZOJ1576: [Usaco2009 Jan]安全路经Travel(最短路 并查集)
    ZRDay6A. 萌新拆塔(三进制状压dp)
    Python 入门教程 10 ---- Student Becomes the Teacher
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10546721.html
Copyright © 2011-2022 走看看