在一行上:
先输入一个整数
代表字符串的个数;
随后,输入
个长度为
,仅由小写字母构成的字符串
;
随后,输入一个字符串
;
最后,输入一个整数
代表要查找的兄弟单词的序号。
第一行输出一个整数,代表给定的
个字符串中,
的“兄弟单词”的数量;
第二行输出一个字符串,代表将给定的
个字符串中
的“兄弟单词”按字典序排序后的第
小兄弟单词。特别地,如果不存在,则不输出任何内容。
3 abc bca cab abc 1
2 bca
在这个样例中,
的兄弟单词为
、
、
、
、
。其中,标橙色的两个字符串存在于所给定的
个字符串中。
3 a aa aaa a 1
0
在这个样例中,按照定义,字符串
没有兄弟单词。
import sys s = input().split() n=int(s[0]) k=int(s[-1]) target = s[-2] lst =s[1:-2] lst1=[] num =0 for i in lst: if len(i) == len(target): if (sorted(list(target))==sorted(list(i))) and (i!=target): lst1.append(i) num+=1 print(num) lst = sorted(lst1) if k<=len(lst): print(lst[k-1])
def is_bro(word1, word2): if word1 == word2: return False j = False w_l1 = [] w_l2 = [] for i in word1: w_l1.append(i) for i in word2: w_l2.append(i) w_l1 = sorted(w_l1) w_l2 = sorted(w_l2) if w_l1 == w_l2: j = True return j shu_ru = input().split() m = int(shu_ru[0]) # d = shu_ru[1 : m + 1] # 单词待查找列表 x = shu_ru[m + 1] # 单词本身 k = int(shu_ru[m + 2]) # 找第k个字典序排序后的兄弟单词 bro = [] for word in d: if is_bro(x, word): bro.append(word) print(len(bro)) bro = sorted(bro) if k <= len(bro): print(bro[k - 1])
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])
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