定义一个单词的“兄弟单词”为:交换该单词字母顺序(注:可以交换任意次),而不添加、删除、修改原有的字母就能生成的单词。
兄弟单词要求和原来的单词不同。例如: ab 和 ba 是兄弟单词。 ab 和 ab 则不是兄弟单词。
现在给定你 n 个单词,另外再给你一个单词 x ,让你寻找 x 的兄弟单词里,按字典序排列后的第 k 个单词是什么?
注意:字典中可能有重复单词。
数据范围:,输入的字符串长度满足 ,
输入只有一行。 先输入字典中单词的个数n,再输入n个单词作为字典单词。 然后输入一个单词x 最后后输入一个整数k
第一行输出查找到x的兄弟单词的个数m 第二行输出查找到的按照字典顺序排序后的第k个兄弟单词,没有符合第k个的话则不用输出。
3 abc bca cab abc 1
2 bca
6 cab ad abcd cba abc bca abc 1
3 bca
abc的兄弟单词有cab cba bca,所以输出3 经字典序排列后,变为bca cab cba,所以第1个字典序兄弟单词为bca
list_input = input().split() n = int(list_input[0]) x = list_input[-2] k = int(list_input[-1]) list_dict_in = list_input[1: -2] list_brother = [] for word in list_dict_in: if (word != x) and (sorted(word) == sorted(x)): list_brother.append(word) print(len(list_brother)) if k <= len(list_brother): list_brother.sort() print(list_brother[k-1])
a = list(input().split(' ')) n = int(a[0]) k = int(a[-1]) mode = a[-2] a1 = a[1:-2] n_b = 0 b = [] for i in a1: if len(i) == len(mode) and i != mode: if sorted(i) == sorted(mode): n_b += 1 b.append(i) print(n_b) if k <= len(b): print(sorted(b)[k-1])
inp = list(map(str, input().split())) n = int(inp[0]) k = int(inp[-1]) target_word = inp[-2] dic = inp[1:-2] def is_brother(word1, word2): return sorted(word1) == sorted(word2) and word1 != word2 def find_brother_words(word_list, target_word, k): brother_words = [word for word in word_list if is_brother(target_word, word)] brother_words.sort() if k <= len(brother_words): return len(brother_words), brother_words[k - 1] else: return len(brother_words), None count, result_word = find_brother_words(dic, target_word, k) print(count) if result_word != None: print(result_word)
while True: try: # 读取输入 all = input().split(" ") # 所有待判断字符串 s = all[1:-2] # 兄弟字符串 bro = all[-2] # 排序后的兄弟字符串的顺序 order = int(all[-1]) # 定义判断兄弟字符串的函数 def judge_bro(s1, s2): if s1 == s2: return False if sorted(s1) != sorted(s2): return False else: return True final = [] # 判断所有字符串是否为兄弟字符串,是则加入final列表,最后排序 for i in s: if judge_bro(i, bro): final.append(i) final.sort() # 输出兄弟字符串列表的长度 print(len(final)) # 输出第order个兄弟字符串(如果有的话) if len(final) >= order: print(final[order - 1]) except EOFError: break
import sys for line in sys.stdin: inputs = line.rstrip("\n").split(" ") n = int(inputs[0]) words = inputs[1:-2] x = inputs[-2] k = int(inputs[-1]) # words.sort() brother_words = [] for word in words: if sorted(word) == sorted(x) and word != x: brother_words.append(word) brother_words.sort() print(len(brother_words)) if k <= len(brother_words): print(brother_words[k - 1])
from copy import deepcopy while 1: try: line = input().split() x, k = line[-2], int(line[-1]) dic = [c for c in line[1 : int(line[0]) + 1]] brother = [] diccp = deepcopy(dic) xcp = sorted(deepcopy(x)) for d, dcp in zip(dic, diccp): if len(d) == len(x): if d != x and sorted(dcp) == xcp: brother.append(d) print(len(brother)) print(sorted(brother)[k - 1]) except: break
import sys for line in sys.stdin: str_list = line.split() word_num, word, k = str_list[0], str_list[-2], int(str_list[-1]) word_list = str_list[1:-2] sort_word_list = sorted(word) brother_list = [] for w in word_list: if sorted(w) == sort_word_list: if w != word: brother_list.append(w) print(len(brother_list)) # 兄弟单词个数 brother_list = sorted(brother_list) # 排序兄第单词列表 if k - 1 < len(brother_list): print(brother_list[k - 1]) # 第k个兄弟单词
import sys for line in sys.stdin: ipt = line[:-1].split(' ') k = int(ipt[-1]) slist = [] jgs = sorted(ipt[-2]) for s in ipt[1:-2]: if s!=ipt[-2]: if sorted(s)==jgs: slist.append(s) print(len(slist)) if k<=len(slist): ks = sorted(slist)[k-1] print(ks)
inputs = input().split(' ') words = inputs[1:-2] key_word = inputs[-2] key_word_sort = sorted(key_word) res_index = int(inputs[-1]) res=[] for i in words: if i == key_word: continue if sorted(i) == key_word_sort: res.append(i) res.sort() length = len(res) print(length) if 0 <= res_index-1 < length: print(res[res_index-1])
while True: try: s=input().strip().split() n=int(s[0]) dic = s[1:-2:] x=s[-2] k=int(s[-1]) m=0 l=[] for i in dic: if sorted(x)==sorted(i) and x!=i: m+=1 l.append(i) print(m) print(sorted(l)[k-1])#兄弟单词列表先排序,再取第K个 except: break