首页 > 试题广场 >

查找兄弟单词

[编程题]查找兄弟单词
  • 热度指数:383238 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
定义一个单词的“兄弟单词”为:交换该单词字母顺序(注:可以交换任意次),而不添加、删除、修改原有的字母就能生成的单词。
兄弟单词要求和原来的单词不同。例如: ab 和 ba 是兄弟单词。 ab 和 ab 则不是兄弟单词。
现在给定你 n 个单词,另外再给你一个单词 x ,让你寻找 x 的兄弟单词里,按字典序排列后的第 k 个单词是什么?
注意:字典中可能有重复单词。

数据范围:,输入的字符串长度满足

输入描述:
输入只有一行。
先输入字典中单词的个数n,再输入n个单词作为字典单词。
然后输入一个单词x
最后后输入一个整数k


输出描述:
第一行输出查找到x的兄弟单词的个数m
第二行输出查找到的按照字典顺序排序后的第k个兄弟单词,没有符合第k个的话则不用输出。
示例1

输入

3 abc bca cab abc 1

输出

2
bca
示例2

输入

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])

编辑于 2024-04-24 13:33:44 回复(0)
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])

编辑于 2024-03-26 18:45:17 回复(0)
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)

发表于 2023-11-27 22:20:31 回复(0)
s = input().split(" ")
rnk = []
for value in s[1:-2]:
    if value != s[-2] and sorted(value) == sorted(s[-2]):
        rnk.append(value)
print(len(rnk))
if len(rnk) > int(s[-1]):
    print(sorted(rnk)[int(s[-1]) - 1])


发表于 2023-11-23 18:00:41 回复(0)
a = input().strip().split()
b = a[1:-2]
x = a[-2]

c = []
for i in b:
    if sorted(list(i))==sorted(list(x)) and i != x:
        c.append(i)
print(len(c))
if int(a[-1]) <= len(c):
    print(sorted(c)[int(a[-1])-1])
发表于 2023-10-17 16:36:07 回复(0)
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

发表于 2023-09-16 16:45:44 回复(0)
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])

发表于 2023-09-12 18:32:55 回复(0)
import sys

a = (input().split(' '))
b = a[1:-2]
c= []
d = sorted(list(a[-2]))
for i in b:
    if len(i)==len(a[-2]) and d==sorted(list(i)) and i!=a[-2]:
            c.append(i)

c = sorted(c)
print(len(c))
if len(c)>int(a[-1]):
    print(c[int(a[-1])-1])

发表于 2023-09-01 23:14:00 回复(0)
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

发表于 2023-07-28 22:14:48 回复(0)
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个兄弟单词

发表于 2023-07-05 16:28:49 回复(0)
danci = input().split()
brother = []
coun = 0
for i in danci[1:-2]:
    if sorted(i) == sorted(danci[-2]) and i != danci[-2]:
        coun += 1
        brother.append(i)
print(coun)
brother = sorted(brother)
if int(danci[-1]) <= len(brother):
    print(brother[int(danci[-1]) - 1])

发表于 2023-06-28 16:17:57 回复(0)
a = input().split()
n = a[0]
l1 = a[1:-2]
word = a[-2]
num = int(a[-1])
l2 = []

for i in l1:
    if sorted(i) == sorted(word) and i != word:
        l2.append(i)
l2 = sorted(l2)

print(len(l2))
if num <= len(l2):
    print(l2[num-1])
发表于 2023-06-23 01:26:15 回复(0)
看了好久才发现自己题目看错了,仔细审题太重要了!!!
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)



发表于 2023-05-14 11:30:34 回复(0)
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])

发表于 2023-04-17 13:35:20 回复(0)
1、兄弟单词列表先排序,再取第K个
2、如果没有兄弟单词,不输出
3、如果有兄弟单词,但是找不到第K个,只输出兄弟单词的数量,不输出兄弟单词
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

发表于 2023-04-16 22:53:45 回复(0)