# 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)