n = 5 sou = [['cloxy',3,0],['kcotd',2,1],['apqud',2,0],['cxmnu',1,1],['bldwz',1,1]] res = [] st = [chr(i) for i in range(97,123)] def find(sou,st,ans): k = len(ans) if k == n: for i in range(len(sou)): if sou[i][1] != 0 or sou[i][2] != 0: break else: res.append(ans) else: for i in st: temp = [[x for x in sou[i]] for i in range(len(sou))] for j in range(len(sou)): if sou[j][0][k] == i: temp[j][1] -= 1 if temp[j][1] == -1: break elif i in sou[j][0]: temp[j][2] -=1 if temp[j][2] == -1: break else: temp1 = st[:] temp1.remove(i) find(temp,temp1,ans+i) find(sou,st,'') print(res[0]) —————————————————————————————————————————— 因为不知道输入的具体格式我就直接把例子输入进来了,就是一个剪枝,评论字数有限大家凑活着看吧
2 1

相关推荐

牛客网
牛客企业服务