#查找兄弟单词#

import java.util.Scanner;

// 注意类名必须为 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();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }
        String line = in.nextLine();
        String[] strs = line.split(" ");
        int len = strs.length;
        String word = strs[len - 2];
        int index = 0;
        String[] broWords = new String[len];
        
        for (int i = 1; i < len - 2; i++) {
            if (word.length() != strs[i].length()) {
                continue;
            }
            boolean isBro = true;
            for (int j = 0; j < word.length(); j++) {
                if (!strs[i].contains(String.valueOf(word.charAt(j))) || getCharCount(strs[i], word.charAt(j)) != getCharCount(word, word.charAt(j))) {
                    isBro = false;
                }
            }
            if (isBro && !strs[i].equals(word)) {
                broWords[index] = strs[i];
                index++;
            }
        }
        int count = index;
        for (int i = 0; i < index; i++) {
            for (int j = i + 1; j < index; j++) {
                if (broWords[j] == null) {
                    continue;
                }
                if (broWords[i] == null) {
                    broWords[i] = broWords[j];
                    count++;
                }
                if (broWords[i].equals(broWords[j])) {
                    broWords[j] = null;
                    count--;
                    continue;
                }
                if (broWords[i].compareTo(broWords[j]) > 0) {
                    String temp = broWords[i];
                    broWords[i] = broWords[j];
                    broWords[j] = temp;
                }
            }

        }
        System.out.println(index);
        int ind = Integer.parseInt(strs[len - 1]);
        if (ind <= count) {
            System.out.print(broWords[ind - 1]);
        }
    }
    private static int getCharCount(String s, char c) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c) {
                count++;
            }
        }
        return count;
    }
}

全部评论

相关推荐

哇哇的菜鸡oc:他这不叫校招offer,而是实习offer
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务