定义一个单词的“兄弟单词”为:交换该单词字母顺序(注:可以交换任意次),而不添加、删除、修改原有的字母就能生成的单词。
兄弟单词要求和原来的单词不同。例如: ab 和 ba 是兄弟单词。 ab 和 ab 则不是兄弟单词。
现在给定你 n 个单词,另外再给你一个单词 x ,让你寻找 x 的兄弟单词里,按字典序排列后的第 k 个单词是什么?
注意:字典中可能有重复单词。
数据范围:,输入的字符串长度满足 ,
输入只有一行。 先输入字典中单词的个数n,再输入n个单词作为字典单词。 然后输入一个单词x 最后输入一个整数k
第一行输出查找到x的兄弟单词的个数m 第二行输出查找到的按照字典顺序排序后的第k个兄弟单词,没有符合第k个的话则不用输出。
3 abc bca cab abc 1
2 bca
7 cab ad abcd cba abc bca bca abc 3
4 cab
abc的兄弟单词有cab cba bca bca,所以输出4 经字典序排列后,变为bca bca cab cba,所以第3个字典序兄弟单词为cab
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));} } }
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)); } } } }
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)); } } } }
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)); } } }
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)); } } }
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); } }
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]); } } }
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)); } } } }
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; } }
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]); } } } }
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; } }
import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String line = sc.nextLine(); sc.close(); String[] strs = line.split(" "); int number = Integer.parseInt(strs[0]); int seq = Integer.parseInt(strs[strs.length - 1]); String tragetStr = strs[strs.length - 2]; ArrayList<String> brotherList = new ArrayList<>(); for (int i = 1; i <= number; i++) { if (isBrother(tragetStr, strs[i])) { brotherList.add(strs[i]); } } System.out.println(brotherList.size()); Collections.sort(brotherList); System.out.println(brotherList.get(seq - 1)); } public static boolean isBrother(String s1, String s2) { char[] chars1 = s1.toCharArray(); char[] chars2 = s2.toCharArray(); Arrays.sort(chars1); Arrays.sort(chars2); if (chars1.length != chars2.length) { return false; } else if (s1.equals(s2)) { return false; } else { for (int i = 0; i < chars1.length; i++) { if (chars1[i] != chars2[i]) return false; } } return true; } }这题很奇怪,如果叫你输出第n个兄弟单词,假如没有这么多怎么搞,题干里面没有说明啊。
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.hasNext()) { // 注意 while 处理多个 case int n = in.nextInt(); List<String> list = new ArrayList<>(); for (int i = 0; i < n ; i++) { list.add(in.next()) ; } String x = in.next(); int k = in.nextInt(); in.nextLine(); int count = 0; String index = ""; char[] xs = x.toCharArray(); Collections.sort(list); for (String str : list) { if (x.equals(str) || x.length() != str.length()) { continue; } char[] strs = str.toCharArray(); Arrays.sort(xs); Arrays.sort(strs); if (!Arrays.equals(strs, xs)) { continue; } count++; if (count == k) { index = str; } } System.out.println(count); System.out.println(index); } } }