首页 > 试题广场 >

查找兄弟单词

[编程题]查找兄弟单词
  • 热度指数:430269 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}定义一个由小写字母构成的字符串 s 的“兄弟单词”为:任意交换 s 中两个字母的位置,得到的新字符串,且其与 s 不同。
\hspace{15pt}现在,对于给定的 n 个字符串 s_1, s_2, \dots, s_n 和另一个单独的字符串 x ,你需要解决两个问题:
\hspace{23pt}\bullet\,统计这 n 个字符串中,有多少个是 x 的“兄弟单词”;
\hspace{23pt}\bullet\,将这 n 个字符串中 x 的“兄弟单词”按字典序从小到大排序,输出排序后的第 k 个兄弟单词。特别地,如果不存在,则不输出任何内容。

\hspace{15pt}从字符串的第一个字符开始逐个比较,直到找到第一个不同的位置,通过比较这个位置字符的字母表顺序得出字符串的大小,称为字典序比较。

输入描述:
\hspace{15pt}在一行上:
{\hspace{20pt}}_\texttt{1.}\,先输入一个整数 n \left(1 \leqq n \leqq 1000\right) 代表字符串的个数;
{\hspace{20pt}}_\texttt{2.}\,随后,输入 n 个长度为 1 \leqq {\rm length}(s_i) \leqq 10 ,仅由小写字母构成的字符串 s_1, s_2, \dots, s_n
{\hspace{20pt}}_\texttt{3.}\,随后,输入一个字符串 x
{\hspace{20pt}}_\texttt{4.}\,最后,输入一个整数 k \left(1 \leqq k \leqq n\right) 代表要查找的兄弟单词的序号。


输出描述:
\hspace{15pt}第一行输出一个整数,代表给定的 n 个字符串中,x 的“兄弟单词”的数量;
\hspace{15pt}第二行输出一个字符串,代表将给定的 n 个字符串中 x 的“兄弟单词”按字典序排序后的第 k 小兄弟单词。特别地,如果不存在,则不输出任何内容。
示例1

输入

3 abc bca cab abc 1

输出

2
bca

说明

\hspace{15pt}在这个样例中,x 的兄弟单词为 \texttt{\texttt{\texttt{\texttt{\texttt{ 。其中,标橙色的两个字符串存在于所给定的 n 个字符串中。
示例2

输入

3 a aa aaa a 1

输出

0

说明

\hspace{15pt}在这个样例中,按照定义,字符串 \texttt{ 没有兄弟单词。
以为只能交换一次两个字母,做了一晚上怎么调都通过不了,遇到好几道题目描述有问题的浪费时间了。。。。。。
发表于 2025-03-08 00:20:20 回复(0)
任意交换 s 中两个字母的位置
不等于
任意交换 s 中字母的位置
你告诉我abc怎么通过一次“任意交换 s 中两个字母的位置”变换成bca?
出题至少要学好语文吧?
发表于 2025-01-17 00:22:04 回复(4)
这道题关于兄弟单词的定义描述有严重的本质问题,根据定义,s的“兄弟单词”为:任意交换 s 中两个字母的位置,得到的新字符串,且其与 s 不同。那么对于示例中的字符串 abc,橙色的bca 和cab均不是其兄弟单词。abc的兄弟单词通过交换字母顺序,可以发现只有交换ac,交换bc,交换ab三种情况依次得到的:cba,acb和bac三种,并没有bca和cab。所以本题目为错题
发表于 2025-01-06 13:37:24 回复(1)
source=input().split(' ')
count=0
res=[]
for i in range(1,len(source)-2):
    if source[i]!=source[-2] and sorted((source[i]))==sorted(source[-2]):
        count+=1
        res.append(source[i])
print(count)
if len(res)>int(source[-1]):
    print(sorted(res)[int(source[-1])-1])

发表于 2024-11-25 18:06:08 回复(0)
# 读取单行输入并且split成单个word
words = input().split()

# 提取total(单词总数x); x(目标单词x); k(字典排序后k个单词)
total = int(words[0])
x = words[-2]
k = int(words[-1])

# list需要查找的全部单词(第二个单词至倒数第二个单词)
words_list = words[1:-2]

# initialize一个list储存查找出来的兄弟单词
output_list = []

# 分别兄弟单词
for word in words_list:
    # 兄弟单词和x不应该完全一样,但是顺序要发生变化
    if (word != x) and (sorted(word) == sorted(x)):
        output_list.append(word)

# 重新sort兄弟单词list
output_list.sort()

# 输出兄弟单词list count
print(len(output_list))

# 打印第k个单词(如果存在的话)
if k <= len(output_list):
    print(output_list[k - 1])

发表于 2024-11-07 12:13:02 回复(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])

发表于 2024-07-12 14:14:04 回复(0)
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])

发表于 2024-06-19 00:16:03 回复(0)
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)