在一行上:
先输入一个整数
代表字符串的个数;
随后,输入
个长度为
,仅由小写字母构成的字符串
;
随后,输入一个字符串
;
最后,输入一个整数
代表要查找的兄弟单词的序号。
第一行输出一个整数,代表给定的
个字符串中,
的“兄弟单词”的数量;
第二行输出一个字符串,代表将给定的
个字符串中
的“兄弟单词”按字典序排序后的第
小兄弟单词。特别地,如果不存在,则不输出任何内容。
3 abc bca cab abc 1
2 bca
在这个样例中,
的兄弟单词为
、
、
、
、
。其中,标橙色的两个字符串存在于所给定的
个字符串中。
3 a aa aaa a 1
0
在这个样例中,按照定义,字符串
没有兄弟单词。
一个笨方法,建立一个字典记录被查找的单词,遍历时再建立一个临时字典 while True: try: lst = input().split() lst1 = [] num_1 = int(lst[0]) num_2 = int(lst[-1]) hash = {} for i in range(len(lst[-2])): if lst[-2][i] not in hash: hash[lst[-2][i]]=1 else: hash[lst[-2][i]] += 1 for i in range(1, num_1 + 1): if lst[i] == lst[-2]: continue else: hash1 = {} for j in range(len(lst[i])): if lst[i][j] not in hash1: hash1[lst[i][j]] = 1 else: hash1[lst[i][j]] +=1 if hash==hash1: lst1.append(lst[i]) print(len(lst1)) print(sorted(lst1)[num_2 - 1]) except: break
all_char=input() all_char=all_char.split(' ') all_len=len(all_char) N=eval(all_char[0]) candidate =all_char[1:all_len-2] search_char=all_char[all_len-2] k=eval(all_char[all_len-1]) search_char_sort=sorted(search_char) count=0 brother_char=[] for c in candidate: if c!=search_char: c_sort=sorted(c) if c_sort==search_char_sort: count+=1 brother_char.append(c) print(count) brother_char=sorted(brother_char) if k<=len(brother_char): print(brother_char[k-1])
while 1: try: ls = input().split() n = int(ls[0]) k = int(ls[-1]) x = ls[-2] l = ls[1 : n + 1] co = 0 p = [] for i in l: if i != x and sorted(i) == sorted(x): p.append(i) co += 1 p.sort() print(co) if p[k - 1]: print(p[k - 1]) except: break
if i != x and sorted(x) == sorted(i):你们看我这个简单吗?
s = input().split() x = s[-2] k = int(s[-1]) h = s[1:-2] bro = [] for i in h: if i != x and sorted(x) == sorted(i): bro.append(i) j = len(bro) if j < k: print(j) else: new_bro = sorted(bro) for i in range(j): if i+1 == k: print(j) print(new_bro[i])
import sys def find_brother_word(brother_word: list, x_word: str) -> list: x_word_dic = {} for s in x_word: if s not in x_word_dic: x_word_dic[s] = 1 else: x_word_dic[s] = x_word_dic[s] + 1 brother_list = [] for item in brother_word: brother_dic = {} for s in item: if s not in brother_dic: brother_dic[s] = 1 else: brother_dic[s] = brother_dic[s] + 1 if item != x_word and brother_dic == x_word_dic: brother_list.append(item) return brother_list if __name__ == '__main__': for line in sys.stdin: a = line.split() brother_word_num = int(a[0]) brother_word = a[1:(brother_word_num + 1)] x_word_num = int(a[-1]) x_word = a[-2] brother_list = find_brother_word(brother_word, x_word) brother_list.sort() print(len(brother_list), end='\n') if len(brother_list) >= x_word_num: print(brother_list[x_word_num-1])
# 判断单词s1和s2是否是兄弟单词 0否 1是 def is_brother(s1,s2): n1,n2=len(s1),len(s2) if n1!=n2: return 0 if n1==n2: L1={x:0 for x in s1} L2={x:0 for x in s2} for x in s1: L1[x]+=1 for x in s2: L2[x]+=1 if sorted(L1.items())==sorted(L2.items()) and s1!=s2: return 1 else: return 0 while True: try: S0=input().split() n=int(S0[0]) x=S0[-2] k=int(S0[-1]) S=S0[1:n+1] L=[] #记录兄弟单词 for a in S: if is_brother(x,a)==1: L.append(a) n=len(L) print(n) if n>=k: L1=sorted(L) print(L1[k-1]) except: break
a = input().split() b = a[1 : len(a) - 2] # n个单词 x = a[len(a) - 2] x_len = len(x) w = 0 # 兄弟字母的个数 t = [] for i in b: if len(i) == x_len: if i == x: continue else: m = 0 for n in i: if n in x and i.count(n) == x.count(n): m += 1 else: continue if m == x_len: w += 1 t.append(i) else: continue print(w) t1 = sorted(t, key=lambda x: x) if len(t1) > int(a[-1]): print(t1[int(a[-1]) - 1])
a = input().split() a1 = a[0] a2 = a[-1] a3 = a[-2] aa = a[1:-2] d = 0 e = [] for b in aa: if len(b) == len(a3): for c in range(len(a3)): if a3[c] in b and b.count(a3[c]) == a3.count(a3[c]): pass else: break if c == len(a3)-1 and b != a3: d += 1 e.append(b) print(d) try: print(sorted(e)[int(a2)-1]) except: pass
while True: try: s = input().split(" ") word_dic, ref, k = s[1:-2], s[-2], int(s[-1]) # 判断是否为兄弟单词 tmp = [] for item in word_dic: if item != ref and sorted(item) == sorted(ref): tmp.append(item) print(len(tmp)) print(sorted(tmp)[k-1]) except: break
while True: try: ls = [i for i in input().split(" ")] n, candidates, target, k = int(ls[0]), ls[1:int(ls[0])+1], ls[int(ls[0])+1], int(ls[-1]) res = 0 tem = [] for item in candidates: if sorted(item) == sorted(target) and item != target: res += 1 tem.append(item) tem = sorted(tem) print(res) if k <= len(tem): print(tem[k-1]) except: break