fork(2) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. public class Main {
  6. public static void main (String[] args) throws java.lang.Exception {
  7. Scanner in = new Scanner(System.in);
  8. String labels = in.nextLine();
  9. String labelsMixed = in.nextLine();
  10. String[] labelsArray = labels.split(" ");
  11. StringBuilder sb = new StringBuilder();
  12. LinkedHashMap<String, String> labelsFound = new LinkedHashMap<String, String>();
  13.  
  14.  
  15. for (String s : labelsMixed.split("")) {
  16. sb.append(s);
  17. String labelFound = findLabel(labelsArray, sb.toString());
  18. if (labelFound != null) {
  19. labelsFound.put(sb.toString(), labelFound);
  20. sb = new StringBuilder();
  21. }
  22. }
  23.  
  24. for (Map.Entry<String, String> entry : labelsFound.entrySet()) {
  25. System.out.print(entry.getKey().concat(" "));
  26. }
  27.  
  28. System.out.println();
  29.  
  30. for (Map.Entry<String, String> entry : labelsFound.entrySet()) {
  31. System.out.print(entry.getValue().concat(" "));
  32. }
  33. }
  34.  
  35. private static String findLabel(String[] array, String anagram) {
  36. String[] anagramArray = anagram.split("");
  37. String[] wordArray;
  38.  
  39. for (String word : array) {
  40. wordArray = word.split("");
  41.  
  42. Arrays.sort(anagramArray);
  43. Arrays.sort(wordArray);
  44.  
  45. if (Arrays.deepEquals(wordArray, anagramArray)) {
  46. return word;
  47. }
  48. }
  49. return null;
  50. }
  51. }
Success #stdin #stdout 0.13s 380672KB
stdin
stol stul dverj krovatj kompjuter
vatjkrotulsredvjlostkompterju
stdout
vatjkro tuls redvj lost kompterju 
krovatj stul dverj stol kompjuter