【名词解释】
在一行上依次输入:
一个整数
代表字符串的个数;
个长度为
,仅由小写字母构成的字符串
;
一个长度为
,仅由小写字母构成的字符串
;
一个整数
代表要查找的第
小的兄弟单词的序号。
第一行输出一个整数,代表给定的
个字符串中,
的“兄弟单词”的数量;
第二行输出一个字符串,代表将给定的
个字符串中
的“兄弟单词”按字典序排序后的第
小兄弟单词。特别地,如果不存在,则不输出任何内容(完全省略第二行)。
3 abc bca cab abc 1
2 bca
在这个样例中,
的兄弟单词为
、
、
、
、
。其中,标橙色的两个字符串存在于所给定的
个字符串中。第
小的兄弟单词为
。
3 a aa aaa a 1
0
在这个样例中,按照定义,字符串
没有兄弟单词。
本题已于下方时间节点更新,请注意题解时效性:
1. 2025-05-30 更新题面。
2. 2024-12-29 更新题面。
import sys # def searchWords(self): inputli = list(input().split()) n = int(inputli[0]) dic = inputli[1:n+1] k = int(inputli[-1]) targWord = inputli[-2] count = 0 broWord = list() for i in range(len(dic)): # print(sorted(dic[i])) if (sorted(dic[i]) == sorted(targWord)) and (dic[i]!=targWord): count +=1 broWord.append(dic[i]) # if (count == k) : # broWord = dic[i] print(count) if (count > 0) and (count > k) : print(broWord[k-1])
while True: try: # 获取信息 a = input().split(' ') #print(a) num = a[0] k = int(a[-1]) target = a[-2] li = a[1:-2] ans_list = [] # 去掉和目标单词相同的单词 while target in li: li.remove(target) # 用sorted和list判断字母组成是否相同 target_list = sorted(list(target)) for word in li: if sorted(list(word)) == target_list: ans_list.append(word) # 对符合要求的单词进行排序 ans_list.sort() #print(ans_list) print(len(ans_list)) # 题目中没有写的条件,可能会出现溢出 if len(ans_list) != 0 and k <= len(ans_list): print(ans_list[int(k)-1]) except: break题目中没有写出可能会有溢出现象,不过也没有写k一定是满足要求的。
length,*w_list,word,k = input().split(" ") def is_brother(w1, w2): if w1 != w2 and sorted(w1) == sorted(w2): return True else: return False def solve(arr, word, k): dictionary = sorted(arr) find = [] for w in dictionary: if is_brother(w, word): find.append(w) if (len(find) <= int(k)): print(len(find)) else: print(len(find)) print(sorted(list(find))[int(k)-1]) solve(w_list, word, k)
str1 = input() str1 = str1.split() n = int(str1[0]) #记录n个单词 k = int(str1[-1]) #记录最终数字k del str1[0] del str1[-1]# 删除数字项 check = str1[-1] #记录待查找单词 ind =[] cc = list(check) cc = set(cc) cc = list(cc) cc =''.join(cc) #去除待查找单词重复项 for i in range (len(cc)): ind.append(check.count(cc[i])) #记录每个字母出现次数 buffer = str1[-1] del str1[-1] #删除查找单词 check = list(check) record =[] for i in range (len(str1)): c=0 for ii in range (len(cc)):#逐个与新单词对比,要同时满足长度一致,出现次数一致,及含有此字母项则计数器+1 if cc[ii] in list(str1[i]) and len(check) == len(list(str1[i])) and str1[i] != buffer and str1[i].count(cc[ii]) == ind[ii]: c = c +1 if c == len(cc): #计数器=字母总类别数代表全类别字母通过 record.append(str1[i]) record = list(record) print(len(record)) record = sorted(record) if 0<= k-1 <= len(record) and record != []: #防止没有符合的情况不输出 print(record[k-1])
6 adbcb但是,这里的6个是:['adbcb', 'bacbd', 'badbc', 'bcbda', 'bcbda', 'cdbab'],请注意有重复单词。
while True: try: s=input().split(" ") n=int(s.pop(0)) k=int(s.pop()) tar=s.pop() #重复单词也算兄弟单词,这个题目没有太明确说明 #s = list(set(s)) res = [] for item in s: if len(item)==len(tar) and item!=tar and sorted(item)==sorted(tar): res.append(item) res=sorted(res) print(len(res)) print(res) if res and len(res)>k: print(res[k-1]) except: break
def is_brother(word, str): return (sorted(word) == sorted(str)) and (word != str) l = input().split() n, l, str, k = int(l[0]), l[1:-2], l[-2], int(l[-1]) brother = [] for word in l: if is_brother(word, str): brother.append(word) brother.sort() print(len(brother)) if k < len(brother): print(brother[k-1])
while True: try: a = input().split() n = a[0] x = a[-2] k = int(a[-1]) a = a[1:-2] m = [] for i in a: if x != i: new_i = sorted(i) new_x = sorted(x) if new_i == new_x: m.append(i) print(len(m)) if len(m) >= k: new_m = sorted(m) print(new_m[k-1]) except: break通过了,但是不知道有没有什么漏洞
def brother(letter_list, search_str, index_bro): search_list = [] sort_search_str = ''.join(sorted(search_str)) for i in letter_list: if search_str != i: sorted_bro_str = ''.join(sorted(i)) if sorted_bro_str == sort_search_str: search_list.append(i) print(len(search_list)) sort_search_list = sorted(search_list) print(sort_search_list[int(index_bro)-1]) while True: try: a_list = input().split() letter_list = a_list[1:-2] search_str = a_list[-2] index_brother = a_list[-1] brother(letter_list, search_str, index_brother) except: break
while True: try: new_list = [] list1 = input().split() num1 = int(list1.pop()) str1 = list1.pop() num2 = list1.pop(0) for x in list1: a = "" if x != str1: if len(x) == len(str1): for y in x: if x.count(y) == str1.count(y): a += y if len(a) == len(x): new_list.append(a) new_list.sort() print(len(new_list)) print(new_list[num1-1]) except: break
while True: try: word_nums, *words, lookup_word, bro_num = input().split() bro_num = int(bro_num) bro_words = [] for word in words: if word == lookup_word: continue if sorted(word) == sorted(lookup_word): bro_words.append(word) bro_words.sort() print(len(bro_words)) if bro_words and bro_num <= len(bro_words): print(bro_words[bro_num-1]) except: break
def brother(s1, s2): if s1 == s2: return False else: s1list = sorted([x for x in s1]) s2list = sorted([y for y in s2]) if s1list == s2list: return True return False line = list(input().split()) n = int(line[0]) s1 = line[-2] count = 0 sindex = [] finbro = int(line[-1]) for i in range(1,n+1): s = line[i] if brother(s1, s): count += 1 sindex.append(i) print(count) if sindex: indexf = sindex[finbro-1] print(line[indexf])
while True: try: ss=input().split() n=int(ss[0]) dict=ss[1:n+1] s=ss[-2] m=int(ss[-1]) a=[] for i in dict: if len(i)==len(s) and i!=s and sorted(i)==sorted(s): a.append(i) print(len(a)) if a and m<=len(a): print(sorted(a)[m-1]) except: break真的服,题目输出没讲明白,浪费半天调试。。。