先构建一个识别是否为兄弟单词的方法,利用此方法获得兄弟单词列表。
def isBrotherWord(check_word, example): if check_word == example: return False check_word = list(check_word) try: for i in list(example): check_word.remove(i) except: return False if check_word == []: return True else: return False while True: try: test_input = input().split(' ') num = int(test_input[0]) word_list = list(test_input[1:num+1]) word = test_input[num+1] k = int(test_input[num+2]) brother_list = [] for i in word_list: if isBrotherWord(i, word): brother_list.append(i) brother_list.sort() print(len(brother_list)) print(brother_list[k-1]) except: break
以为是去重的,才知道原来没这个要求。