解题思路

  1. 兄弟单词的定义:两个单词包含相同的字符,但字符顺序不同
  2. 处理步骤:
    • 对于给定的单词x,找出所有它的兄弟单词
    • 将兄弟单词按字典序排序
    • 输出第k个兄弟单词(如果存在)
  3. 判断兄弟单词的方法:
    • 将两个单词分别排序后比较是否相等
    • 排除与原单词完全相同的情况

代码

def is_brother(word1, word2):
    """判断两个单词是否为兄弟单词"""
    # 相同单词不是兄弟单词
    if word1 == word2:
        return False
    # 长度不同不是兄弟单词
    if len(word1) != len(word2):
        return False
    # 排序后相同则是兄弟单词
    return sorted(word1) == sorted(word2)

while True:
    try:
        # 读取输入
        inputs = input().split()
        n = int(inputs[0])  # 单词个数
        words = inputs[1:n+1]  # 单词列表
        x = inputs[n+1]  # 待查找单词
        k = int(inputs[n+2])  # 第k个兄弟单词
        
        # 找出所有兄弟单词
        brothers = []
        for word in words:
            if is_brother(word, x):
                brothers.append(word)
        
        # 按字典序排序
        brothers.sort()
        
        # 输出结果
        print(len(brothers))
        if len(brothers) >= k:
            print(brothers[k-1])
            
    except EOFError:
        break
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

bool isBrother(string word1, string word2) {
    // 相同单词不是兄弟单词
    if (word1 == word2) return false;
    // 长度不同不是兄弟单词
    if (word1.length() != word2.length()) return false;
    // 排序后相同则是兄弟单词
    sort(word1.begin(), word1.end());
    sort(word2.begin(), word2.end());
    return word1 == word2;
}

int main() {
    int n;
    while (cin >> n) {
        vector<string> words(n);
        for (int i = 0; i < n; i++) {
            cin >> words[i];
        }
        
        string x;
        int k;
        cin >> x >> k;
        
        // 找出所有兄弟单词
        vector<string> brothers;
        for (const string& word : words) {
            if (isBrother(word, x)) {
                brothers.push_back(word);
            }
        }
        
        // 按字典序排序
        sort(brothers.begin(), brothers.end());
        
        // 输出结果
        cout << brothers.size() << endl;
        if (k <= brothers.size()) {
            cout << brothers[k-1] << endl;
        }
    }
    return 0;
}
import java.util.*;

public class Main {
    private static boolean isBrother(String word1, String word2) {
        // 相同单词不是兄弟单词
        if (word1.equals(word2)) return false;
        // 长度不同不是兄弟单词
        if (word1.length() != word2.length()) return false;
        // 排序后比较
        char[] chars1 = word1.toCharArray();
        char[] chars2 = word2.toCharArray();
        Arrays.sort(chars1);
        Arrays.sort(chars2);
        return Arrays.equals(chars1, chars2);
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            String[] words = new String[n];
            for (int i = 0; i < n; i++) {
                words[i] = sc.next();
            }
            
            String x = sc.next();
            int k = sc.nextInt();
            
            // 找出所有兄弟单词
            List<String> brothers = new ArrayList<>();
            for (String word : words) {
                if (isBrother(word, x)) {
                    brothers.add(word);
                }
            }
            
            // 按字典序排序
            Collections.sort(brothers);
            
            // 输出结果
            System.out.println(brothers.size());
            if (k <= brothers.size()) {
                System.out.println(brothers.get(k-1));
            }
        }
    }
}

算法及复杂度

  • 算法:字符串排序 + 比较
  • 时间复杂度:,其中n是单词个数,m是单词的平均长度
  • 空间复杂度:,用于存储兄弟单词列表