为什么python回溯进不了测试用例
class Solution:
def subset(self,s,n):
self.res = []
partial = ""
self.used = [False] * len(s)
self.backtracking(partial,s)
self.res.sort()
return self.res[n-1]
def backtracking(self,partial,s):
if partial:
self.res.append(partial)
if len(partial) == len(s):
return
else:
for i in range(len(s)):
if not self.used[i]:
if not partial&nbs***bsp;ord(s[i]) >= ord(partial[-1]):
if i > 0 and s[i] == s[i-1] and not self.used[i - 1]:
continue
self.used[i] = True
partial+=s[i]
self.backtracking(partial,s)
partial = partial[:-1]
self.used[i] = False
a = input().split()
s = a[0]
n = int(input().split()[0])
print(str(Solution().subset(s,n)))
全部评论
(0) 回帖