小贴士
- 本题目难度适中,理解题意需要浪费一些时间
- 在调式过程中,会发现有越界出现,是由于查找索引大于兄弟单词个数
- 在Java中,个人建议多用已有API,避免重复造轮子
输入:3 abc bca cab abc 1
输出:2
bca
/**
* using BufferedReader is more efficient than Scanner
* @author Steven
* @version 2020-08-10
*/
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args)throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while((str = bf.readLine()) != null){
String[] items = str.split(" ");
int N = Integer.parseInt(items[0]);
String word = items[items.length - 2];
int index = Integer.parseInt(items[items.length - 1]);
List<String> dictlist = new LinkedList<>();
for(int i = 1; i <= N; i++){
if(word.length() == items[i].length() && !word.equals(items[i])){
if(isBrother(word, items[i])){
dictlist.add(items[i]);
}
}
}
Collections.sort(dictlist);
System.out.println(dictlist.size());
if(1 <= index && index <= dictlist.size()){
System.out.println(dictlist.get(index - 1));
}
}
bf.close();
}
private static boolean isBrother(String s1, String s2){
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
return Arrays.equals(c1, c2);
}
}