首页 > 试题广场 >

查找兄弟单词

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

【名词解释】
\hspace{15pt}从字符串的第一个字符开始逐个比较,直至发现第一个不同的位置,比较这个位置字符的字母表顺序,字母序较小的字符串字典序也较小;如果比较到其中一个字符串的结尾时依旧全部相同,则较短的字符串字典序更小。

输入描述:
\hspace{15pt}在一行上依次输入:
\hspace{23pt}\bullet\,一个整数 n \left(1 \leqq n \leqq 10^3\right) 代表字符串的个数;
\hspace{23pt}\bullet\,n 个长度为 1 \leqq {\rm length}(s_i) \leqq 10,仅由小写字母构成的字符串 s_1, s_2, \dots, s_n
\hspace{23pt}\bullet\,一个长度为 1 \leqq {\rm length}(x) \leqq 10,仅由小写字母构成的字符串 x
\hspace{23pt}\bullet\,一个整数 k \left(1 \leqq k \leqq n\right) 代表要查找的第 k 小的兄弟单词的序号。


输出描述:
\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 个字符串中。第 1 小的兄弟单词为 \texttt{
示例2

输入

3 a aa aaa a 1

输出

0

说明

\hspace{15pt}在这个样例中,按照定义,字符串 \texttt{ 没有兄弟单词。

备注:
本题已于下方时间节点更新,请注意题解时效性:
1. 2025-05-30 更新题面。
2. 2024-12-29 更新题面。
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str = in.nextLine();//获取输入的字符串
        String[] arr = str.split(" ");//按照" "分割,获取数组
        int n = Integer.parseInt(arr[0]);//第一个为输入的单词数量
        String[] arr2 = new String[n];//创建单词数组存储单词
        for (int i = 1; i <= n ;i++){//将单词存入单词数组中
            arr2[i-1]=arr[i];
        }
        //倒数第二个为需要对比的单词
        String x = arr[arr.length-2];
        int xLength = x.length();
        //需要对比的单词取出所有的字符,并且进行排序
        char[] xChars = x.toCharArray();
        Arrays.sort(xChars);
        String xSort = new String(xChars);
        //最后一位为要输出第几位兄弟单词
        int k = Integer.parseInt(arr[arr.length-1]);
        //存储arr2中所找到的兄弟单词
        List<String> arr2Brother = new ArrayList<>();
        //列出arr2中含有的x的兄弟单词
        for (int i = 0;i < arr2.length;i++){
            if (arr2[i].length() != xLength || arr2[i].equals(x)){
                //长度不同,必不可能为兄弟单词,相同,也不是兄弟单词
                continue;
            }
            char[] temp = arr2[i].toCharArray();
            Arrays.sort(temp);
            if (new String(xChars).equals(new String(temp))){
                arr2Brother.add(arr2[i]);
            }
        }
        //排序兄弟单词
        arr2Brother.sort(Comparator.naturalOrder());//升序  arr2Brother.sort(null);
        //arr2Brother.sort(Comparator.reverseOrder());//降序
        System.out.println(arr2Brother.size());//输出兄弟单词个数
        if (arr2Brother.size()>0 && k <= arr2Brother.size()){//如果k小于兄弟单词列表长度,则需要输出的兄弟单词存在
            System.out.println(arr2Brother.get(k-1));
        }
        //列出x所有的兄弟单词 效率低
        // List<String> brother = new ArrayList<>(); 
        // List<String> result = new ArrayList<>(); 
        // brotherFind(x.toCharArray(),0,brother);
        // int count = 0;
        // for (int i = 0; i < arr2.length;i++){
        //     if (arr2[i].equals(x) || arr2[i].length() != xLength){//如果是相同的单词或者长度不同,则跳过
        //         continue;
        //     } else if (brother.contains(arr2[i])){
        //         count++;
        //         result.add(arr2[i]);
        //     }
        // }
        // result.sort(null);
        // System.out.println(count);
        // if (k <= result.size() && k > 0) {//如果k小于兄弟单词列表长度,则需要输出的兄弟单词存在
        //     System.out.println(result.get(k-1));
        // }
        
    }

    //列出所有的兄弟单词
    public static void brotherFind(char[] chars,int start ,List<String> result){
        /** 从第start位开始固定,并与之后的字符进行替换 */
        if (start == chars.length){
            result.add(new String(chars));
        } else {
            for (int i = start; i < chars.length; i++){
                //从第start位开始固定,并与之后的字符进行替换
                swap(chars,start,i);
                brotherFind(chars,start+1,result);//递归,找到第start位所有的兄弟单词
                swap(chars,start,i);//将替换过的字符替换回来
            }
        }
        
    }
    public static void swap(char[] chars,int x,int y){
        char temp = chars[x];
        chars[x] = chars[y];
        chars[y] = temp;
    }
}

发表于 2025-06-23 22:46:22 回复(0)

package com.hw.nk;

import java.util.*;

/*
思路:
1. 将字符型x切割成字符串,并统计每个字符串出现了几次
2. 将输入的比较字符串集合,也分别切割成字符串,并统计每个字符串出现了几次
3. 上述的都用TreeMap集合存储,所以是有序的,如果集合equals比较,相等即为兄弟串(因为每种字符只要出现次数一样,不管如何排序都一样的)
4. 当为兄弟串时,存储集合的索引,便于后续输出
*/

public class Test27 {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    String[] s1 = s.split(" ");
    String sourceStr = s1[s1.length-2];
    int k = Integer.parseInt(s1[s1.length-1]);
    List<String> list = new ArrayList();
    for (int i = 1; i < s1.length - 2; i++) {
        list.add(s1[i]);
    }
    TreeMap<Character,Integer> sourceMap = new TreeMap<>();
    for (char c : sourceStr.toCharArray()){
        sourceMap.put(c,sourceMap.getOrDefault(c,0)+1);
    }
    int num = 0;
    List<Integer> result = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        if(!list.get(i).equals(sourceStr)){
            TreeMap<Character,Integer> targetMap = new TreeMap<>();
            for (char c : list.get(i).toCharArray()){
                targetMap.put(c,targetMap.getOrDefault(c,0)+1);
            }
            if(targetMap.equals(sourceMap)){
                result.add(i);
                num++;
            }
        }
    }
    System.out.println(num);
    if(num != 0 && num >= k){
        System.out.println(list.get(result.get(k-1)));
    }
}

}

用例通过率90%,还有10%哪位大佬能帮忙看看

发表于 2025-03-21 19:16:17 回复(2)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int a = in.nextInt();
        List<String> word = new ArrayList<>();
        for (int i = 0; i < a; i++) {
            word.add(in.next());
        }
        String target = in.next();
        int index = in.nextInt();
        List<String> bratherList = new ArrayList<>();

        char[] targetarr = target.toCharArray();
        Arrays.sort(targetarr);

        word.forEach(e-> {
            if (e.length() == target.length() && !e.equals(target)) {
                char[] ee = e.toCharArray();
                Arrays.sort(ee);
                Boolean flag = true;
                for(int i=0;i<ee.length;i++){
                    if(ee[i] != targetarr[i]) flag=false;
                }

                if (flag) {
                    bratherList.add(e);
                }
            }
        });

        bratherList.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        System.out.println(bratherList.size());
        // 如果得到的下标超过兄弟列表数组,不输出
        if((index-1)<bratherList.size()){
        System.out.println(bratherList.get(index - 1));}

    }
}


发表于 2024-10-31 09:12:33 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int cnt = in.nextInt();
        String[] input = new String[cnt];
        // cab ad abcd cba abc bca abc
        for (int i = 0 ; i < cnt ; i++) {
            input[i] = in.next();
        }
        String target = in.next();
        int index = in.nextInt();
        //System.out.println(target);
        ArrayList<String> res = new ArrayList<>();
        for (int i = 0 ; i < cnt ; i++) {
            if (input[i].length() == target.length()) {
                if (isbrother(input[i], target) && !target.equals(input[i])) res.add(input[i]);
            }
        }

        res.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });

        System.out.println(res.size());
        if (index - 1 < res.size()) {
            System.out.println(res.get(index - 1));
        }

    }

    public static boolean isbrother(String a, String  b) {
        char[] a1 = a.toCharArray();
        char[] b1 = b.toCharArray();
        Arrays.sort(a1);
        Arrays.sort(b1);
        return new String(a1).equals(new String(b1));
    }
}
发表于 2024-09-07 16:08:31 回复(0)
import java.util.*;

import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String[] input = in.nextLine().split(" ");
            int length = Integer.parseInt(input[0]);
            String x = input[length+1];
            int index = Integer.parseInt(input[length+2]);
            List<String> list = new ArrayList<>();
            for(int i=0;i<length;i++){
                list.add(input[i+1]);
            }
            // 过滤掉完全一致,以及长度不符合的单词
            List<String> list2 = list.stream().filter(a->((a.compareTo(x)!=0)&&(a.length()==x.length()))).collect(Collectors.toList());
            List<String> list3 = new ArrayList<>();
            // 判断兄弟数
            for(String item : list2){
                String item1 = item;
                String x1 = x;
                boolean isBro = true;
                for(int i =0;i<item.length();i++){
                    String ch = item.charAt(i)+"";
                    // 两者同时去除同样的字符,若去除过程中出现两个单词长度不一致的情况,则不是兄弟数
                    item1 = item1.replace(ch,"");
                    x1 = x1.replace(ch,"");
                    if(x1.length() != item1.length()){
                        isBro = false;
                        break;
                    }
                }
                if(isBro){
                    list3.add(item);
                }
            }
            // 按照字典排序
            list3.sort((a,b)->a.compareTo(b));
            System.out.println(list3.size());
            // 兄弟数不少于1,并且给定下标小于等于兄弟数量,则打印指定下标的兄弟数
            if((index-1)<=list3.size() && (list3.size()!=0)){
                System.out.println(list3.get(index-1));
            }
        }
    }
}

发表于 2024-08-30 00:51:55 回复(0)
//这个题咋回事,逻辑是对的啊,用例只能跑一半。提示说是有6个,我的代码找到了5个单词?哪里有问题啊



import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a  = in.nextInt();
            String  str = in.nextLine().trim();
            String[] resArr = str.split(" ");
            String s = resArr[a];
            int length = s.length();
            char [] sArr  = s.toCharArray();
            Arrays.sort(sArr);
            String  newS = new String(sArr);
            LinkedList<String>  list = new LinkedList<>();
            TreeSet<String> treeSet = new TreeSet<>();
            int b = Integer.valueOf(resArr[a + 1]);
            for (int i = 0 ; i <=a ; i++) {
                if (resArr[i].equals(s)) {
                    continue;
                }
                if (resArr[i].length() == s.length()) {
                    char[] c = resArr[i].toCharArray();
                    Arrays.sort(c);
                    if (newS.equals(new String(c))) {
                        if (!list.contains(resArr[i])) {
                            list.add(resArr[i]);
                        }
                    }
                }

            }
            Collections.sort(list);
            System.out.println(list.size());
            if (list.size() != 0 && list.size() >= b) {
                System.out.println(list.get(b - 1));
            }

        }
    }
}]


发表于 2024-07-23 17:44:15 回复(1)
import java.util.Scanner;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            String[] arr = s.split(" ");
            int target = Integer.parseInt(arr[arr.length-1]);
            String targetStr = arr[arr.length-2];
            String[] targetArr = targetStr.split("");
            Arrays.sort(targetArr);
            List<String> targetList = Arrays.asList(targetArr);
            Map<List<String>,String> map = new HashMap<>();
            map.put(targetList,targetStr);
            List<String> resList = new ArrayList<>();
            for (int i=1;i<arr.length-2;i++) {
                String item = arr[i];
                String[] itemArr = item.split("");
                Arrays.sort(itemArr);
                List<String> itemList = Arrays.asList(itemArr);
                if (map.get(itemList) != null && !targetStr.equals(item)) {
                    resList.add(item);
                }
            }
            System.out.println(resList.size());
            Collections.sort(resList);
            if (target - 1 < resList.size()) {
                System.out.println(resList.get(target - 1));
            }
        }
    }
} 

发表于 2024-07-09 15:03:34 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String nextLine = sc.nextLine();
        String[] strings = nextLine.split(" ");
        int num = Integer.parseInt(strings[0]);
        int find = Integer.parseInt(strings[num + 2]);
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 1; i < strings.length - 2; i++) {
            arrayList.add(strings[i]);
        }
        //Collections.sort(arrayList);
        String target = strings[num + 1];
        char[] chars = target.toCharArray();
        Arrays.sort(chars);
        int sum = 0;
        ArrayList<String> brother = new ArrayList<>();
        for (String s : arrayList) {
            char[] charArray = s.toCharArray();
            Arrays.sort(charArray);
            if (Arrays.equals(charArray, chars) && !s.equals(target)){
                sum++;
                brother.add(s);
                //System.out.println(s);
            }
        }
        System.out.println(sum);
        Collections.sort(brother);
        if (find < brother.size()){
            System.out.println(brother.get(find - 1));
        }
    }
}

发表于 2024-06-29 14:33:07 回复(0)
第六组 说是476个字典数 实际上只给了463个 我真服
发表于 2024-06-17 15:55:12 回复(0)
    public static boolean brotherWord(String word1, String word2) {
        int len1 = word1.length();
        int len2 = word2.length();
        int[] list1 = new int[len1];
        int[] list2 = new int[len2];
        for (int i = 0; i < len1; i++) {
            list1[i] = word1.charAt(i);
        }

        for (int i = 0; i < len2; i++) {
            list2[i] = word2.charAt(i);
        }
        int num1 = Arrays.stream(list1).reduce(1, (int x, int y) -> x * y);
        int num2 = Arrays.stream(list2).reduce(1, (int x, int y) -> x * y);

        if (len1 != len2 || word1.equals(word2)) {
            return false;
        }
        return num1 == num2;
    }
发表于 2024-03-24 23:37:16 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<String> r = new ArrayList();
        while (n -- > 0) {
            r.add(in.next());
        }
        String t = in.next();
        int x = in.nextInt();
        List<String> result = new ArrayList();
        for (String st : r) {
            if (st.length() != t.length() || st.equals(t)) {
                continue;
            }
            // 这里不能用set,会去重,人傻了
            List<String> set = new ArrayList(Arrays.asList(t.split("")));
            for (int i = 0; i < st.length(); ++ i) {
                set.remove(st.charAt(i) + "");
            }
            if (set.isEmpty()){
                result.add(st);
            }
        }
        Collections.sort(result);
        System.out.println(result.size());
        if (x < result.size()) {
            System.out.println(result.get(x - 1));
        }
    }
}

编辑于 2024-03-16 19:08:46 回复(0)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String item = in.next();
            list.add(item);
        }
        Collections.sort(list);
        String target = in.next();
        int index = in.nextInt();
        int count = 0;
        String targetWord = "";
        char[] arr1 = target.toCharArray();
        Arrays.sort(arr1);
        for (String word : list) {

            if (word.equals(target) || word.length() != target.length()) {
                continue;
            }
            char[] arr2 = word.toCharArray();
            Arrays.sort(arr2);
            if (!Arrays.equals(arr1, arr2)) {
                continue;
            }
            count ++;
            if (count == index) {
                targetWord = word;
            }

        }
        System.out.println(count);
        System.out.println(targetWord);
    }
}

发表于 2024-03-08 19:46:00 回复(0)
判断x单词和所给单词是否兄弟 将两者拆成字符数组再Array.sort 进行比较
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int wordSize = in.nextInt();
        String[] wordArray = new String[wordSize];
        for(int i = 0; i < wordSize; i++){
            wordArray[i] = in.next();
        }
        String xWord = in.next();
        int xWordLength = xWord.length();
        char[] xArray = xWord.toCharArray();
        Arrays.sort(xArray);
        int index = in.nextInt();
        List<String> brotherWords = new ArrayList();
        for(int i = 0; i < wordSize; i++){
            String wordElement = wordArray[i];
            char[] charArray = wordElement.toCharArray();
            Arrays.sort(charArray);
            if(wordElement.length() == xWordLength && !wordElement.equals(xWord) && Arrays.equals(charArray,xArray)){
                brotherWords.add(wordElement);
            }
        }
        System.out.println(brotherWords.size());
        Object[] brotherWordArray = brotherWords.toArray();
        Arrays.sort(brotherWordArray);
        if(index <= brotherWords.size()){
            System.out.println(brotherWordArray[index-1]);
        }
    }
}


发表于 2024-01-17 00:50:27 回复(0)
import java.util.*;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String s =  in.nextLine();
            String[] arr = s.split(" ");
            //输入单词个数
            int num = Integer.valueOf(arr[0]);
            //目标匹配单词,并对字符排序
            String target = arr[arr.length - 2];
            char[] targetArray =  target.toCharArray();
            Arrays.sort(targetArray);
            //目标输出索引
            int index = Integer.valueOf(arr[arr.length - 1]);

            List<String> list = new ArrayList();
            for (String word : arr) {
                //完全重复的单词跳过
                if (target.equals(word)) {
                    continue;
                }
                //匹配单词记录在list
                char[] charArray =  word.toCharArray();
                Arrays.sort(charArray);
                if (Arrays.equals(targetArray, charArray)) {
                    list.add(word);
                }
            }
            List<String> listRes = list.stream().sorted().collect(Collectors.toList());

            System.out.println(listRes.size());
            if(listRes.size() >= index){
                 System.out.println(listRes.get(index-1));
            }
        }
    }
}

编辑于 2023-12-14 17:56:07 回复(0)
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<String> list = new ArrayList();
        String[] strs = new String[n];
        for (int i = 0; i < n; i++) {
            strs[i] = in.next();
        }
        String s = in.next();
        int k = in.nextInt();
        int[] nums = new int[256];
        for (int i = 0; i < s.length(); i++) nums[s.charAt(i)]++;
        for (int i = 0; i < strs.length; i++) {
            int[] nums3 = new int[256];
            if (s.equals(strs[i])) continue;
            for (int j = 0; j < strs[i].length(); j++) nums3[strs[i].charAt(j)]++;
            if (compareInt(nums, nums3)) {
                list.add(strs[i]);
            }
        }
        Collections.sort(list);
        System.out.println(list.size());
        if (k < list.size()) {
            System.out.println(list.get(k-1));
        }
    }
    //判断是否相等
    public static boolean compareInt(int[] nums1, int[] nums2) {
        for (int i = 0; i < nums1.length; i++) {
            if (nums1[i] != nums2[i]) {
                return false;
            }
        }
        return true;
    }
}

编辑于 2023-12-08 00:09:27 回复(0)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int num=Integer.parseInt(in.next());
            String[] str=new String[num];
            for (int i = 0; i < num; i++) {
                str[i]=in.next();
            }
            String x=in.next();
            int k=in.nextInt();
            String[] mid=x.split("");
            List<String> list=new ArrayList<>();
            Arrays.sort(mid);
            for (int i = 0; i < num; i++) {
                if (str[i].length()==x.length()){
                    String[] test=str[i].split("");
                    Arrays.sort(test);
                    if (Arrays.equals(test,mid)){
                        if (str[i].equals(x)!=true){
                            list.add(str[i]);

                        }
                    }
                }
            }
            String[] brother=list.toArray(new String[list.size()]);
            Arrays.sort(brother);
            System.out.println(brother.length);
            if (brother.length>k)
            {
            System.out.println(brother[k-1]);
            }
        }
    }
}

发表于 2023-09-07 15:20:51 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class HJ27 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();

        String[] split = line.split(" ");
        int n = Integer.parseInt(split[0]);
        String x = split[split.length - 2];
        int k = Integer.parseInt(split[split.length - 1]);

        ArrayList<String> list = new ArrayList<>();
        // 找出兄弟字符串
        for (int i = 0; i < n; i++) {
            StringBuilder sb = new StringBuilder(split[i + 1]);
            if (isBrother(sb.toString(), x)) {
                list.add(split[i + 1]);
            }
        }

        // 兄弟字符串排序,按照默认的字典序
        Collections.sort(list);

        // 输出第k个
        System.out.println(list.size());
        if (k <= list.size()) {
            System.out.println(list.get(k - 1));
        }
    }

    /**
     * 判断两个字符串是否为兄弟字符串
     *
     * @param str1
     * @param str2
     * @return
     */
    private static boolean isBrother(String str1, String str2) {
        // 如果两个字符串相等,则不为兄弟
        if (str1.equals(str2)) {
            return false;
        }

        // 长度不等不是兄弟字符串
        if (str1.length() != str2.length()) {
            return false;
        }

        // 交换该单词字母顺序(注:可以交换任意次),而不添加、删除、修改原有的字母就能生成的单词
        // 兄弟字符串中元素应该相同,个数也相同
        // 兄弟字符串排序后应该相等
        char[] chars1 = str1.toCharArray();
        char[] chars2 = str2.toCharArray();
        Arrays.sort(chars1);
        Arrays.sort(chars2);
        String s1 = new String(chars1);
        String s2 = new String(chars2);
        if (!s1.equals(s2)) {
            return false;
        }

        return true;
    }
}

发表于 2023-08-07 17:47:04 回复(0)