Source code for submission s484

Fs.java

  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.*;
  4.  
  5.  
  6.  
  7. public class Fs {
  8.  
  9. private static Map<Character, String> morse = new HashMap<Character, String>();
  10.  
  11. private static void initializeMorse() {
  12. morse.put('A', ".-");
  13. morse.put('B', "-...");
  14. morse.put('C', "-.-.");
  15. morse.put('D', "-..");
  16. morse.put('E', ".");
  17. morse.put('F', "..-.");
  18. morse.put('G', "--.");
  19. morse.put('H', "....");
  20. morse.put('I', "..");
  21. morse.put('J', ".---");
  22. morse.put('K', "-.-");
  23. morse.put('L', ".-..");
  24. morse.put('M', "--");
  25. morse.put('N', "-.");
  26. morse.put('O', "---");
  27. morse.put('P', ".--.");
  28. morse.put('Q', "--.-");
  29. morse.put('R', ".-.");
  30. morse.put('S', "...");
  31. morse.put('T', "-");
  32. morse.put('U', "..-");
  33. morse.put('V', "...-");
  34. morse.put('W', ".--");
  35. morse.put('X', "-..-");
  36. morse.put('Y', "-.--");
  37. morse.put('Z', "--..");
  38. morse.put('_', "..--");
  39. morse.put(',', ".-.-");
  40. morse.put('.', "---.");
  41. morse.put('?', "----");
  42. }
  43.  
  44. public static void main(String[] args) {
  45.  
  46. initializeMorse();
  47.  
  48. Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
  49.  
  50. while (sc.hasNextLine()) {
  51.  
  52. String s = sc.next();
  53. StringBuilder sb = new StringBuilder();
  54. ArrayList<Integer> numbers = new ArrayList<Integer>();
  55.  
  56. for (char c : s.toCharArray()) {
  57. String code = morse.get(c);
  58. sb.append(code);
  59. numbers.add(code.length());
  60. }
  61.  
  62. Collections.reverse(numbers);
  63. StringBuilder result = new StringBuilder();
  64.  
  65. int i = 0;
  66. for (int x : numbers) {
  67. String cut = sb.substring(i, i+x);
  68. result.append(findKey(cut));
  69. i += x;
  70. }
  71.  
  72. System.out.println(result);
  73.  
  74. }
  75.  
  76. }
  77.  
  78. private static char findKey(String cut) {
  79. for(Map.Entry<Character, String> en : morse.entrySet()) {
  80. if (en.getValue().equals(cut)) {
  81. return en.getKey();
  82. }
  83. }
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90. }
  91.