#腾讯笔试#8.23 腾讯笔试第二题为什么回溯一个都过不了
import sys
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 or 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)))
import sys
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 or 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)))
全部评论
我本地自测的结果都是对的然后一个都没过,请问一下是为什么呢
相关推荐
别看我了哥们:Xy✌🏻还需要攒结婚奶粉钱,我这种***丝该咋办
点赞 评论 收藏
分享
mmvvcc:没通过你就偷着乐吧,这样的进去你天天受气
点赞 评论 收藏
分享