Source code for submission s745

Main.java

  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.HashMap;
  6.  
  7. public class Main {
  8.  
  9. static HashMap<String,String> MORSE = new HashMap<String,String>();
  10. static HashMap<String,String> REVERSE_MORSE = new HashMap<String,String>();
  11.  
  12. public static void main(String[] args) throws IOException {
  13. String str;
  14. applyMap();
  15. while ((str = reader.readLine()) != null) {
  16. int[] codes = new int[str.length()];
  17. String out = "";
  18. for (int i = 0; i < str.length(); i++) {
  19. String tmp = MORSE.get(str.charAt(i)+"");
  20. codes[i] = tmp.length();
  21. out = out + tmp;
  22. }
  23.  
  24. for (int i = 0; i < codes.length/2; i++) {
  25. int tmp = codes[i];
  26. codes[i] = codes[codes.length-i-1];
  27. codes[codes.length-i-1] = tmp;
  28. }
  29. int total = 0;
  30. for (int i = 0; i < codes.length; i++) {
  31. int tmp_i = codes[i];
  32. String tmp_s = out.substring(total, total+tmp_i);
  33. total += tmp_i;
  34. System.out.print(REVERSE_MORSE.get(tmp_s));
  35. }
  36. System.out.println();
  37. }
  38. }
  39.  
  40. static void applyMap() {
  41.  
  42. MORSE.put("A", ".-");
  43. MORSE.put("B", "-...");
  44. MORSE.put("C", "-.-.");
  45. MORSE.put("D", "-..");
  46. MORSE.put("E", ".");
  47. MORSE.put("F", "..-.");
  48. MORSE.put("G", "--.");
  49. MORSE.put("H", "....");
  50. MORSE.put("I", "..");
  51. MORSE.put("J", ".---");
  52. MORSE.put("K", "-.-");
  53. MORSE.put("L", ".-..");
  54. MORSE.put("M", "--");
  55. MORSE.put("N", "-.");
  56. MORSE.put("O", "---");
  57. MORSE.put("P", ".--.");
  58. MORSE.put("Q", "--.-");
  59. MORSE.put("R", ".-.");
  60. MORSE.put("S", "...");
  61. MORSE.put("T", "-");
  62. MORSE.put("U", "..-");
  63. MORSE.put("V", "...-");
  64. MORSE.put("W", ".--");
  65. MORSE.put("X", "-..-");
  66. MORSE.put("Y", "-.--");
  67. MORSE.put("Z", "--..");
  68. MORSE.put("_", "..--");
  69. MORSE.put(",", ".-.-");
  70. MORSE.put(".", "---.");
  71. MORSE.put("?", "----");
  72.  
  73. REVERSE_MORSE.put(".-", "A");
  74. REVERSE_MORSE.put("-...", "B");
  75. REVERSE_MORSE.put("-.-.", "C");
  76. REVERSE_MORSE.put("-..", "D");
  77. REVERSE_MORSE.put(".", "E");
  78. REVERSE_MORSE.put("..-.", "F");
  79. REVERSE_MORSE.put("--.", "G");
  80. REVERSE_MORSE.put("....", "H");
  81. REVERSE_MORSE.put("..", "I");
  82. REVERSE_MORSE.put(".---", "J");
  83. REVERSE_MORSE.put("-.-", "K");
  84. REVERSE_MORSE.put(".-..", "L");
  85. REVERSE_MORSE.put("--", "M");
  86. REVERSE_MORSE.put("-.", "N");
  87. REVERSE_MORSE.put("---", "O");
  88. REVERSE_MORSE.put(".--.", "P");
  89. REVERSE_MORSE.put("--.-", "Q");
  90. REVERSE_MORSE.put(".-.", "R");
  91. REVERSE_MORSE.put("...","S");
  92. REVERSE_MORSE.put("-","T");
  93. REVERSE_MORSE.put("..-","U");
  94. REVERSE_MORSE.put("...-","V");
  95. REVERSE_MORSE.put(".--","W");
  96. REVERSE_MORSE.put("-..-","X");
  97. REVERSE_MORSE.put("-.--","Y");
  98. REVERSE_MORSE.put("--..","Z");
  99. REVERSE_MORSE.put("..--","_");
  100. REVERSE_MORSE.put(".-.-",",");
  101. REVERSE_MORSE.put("---.",".");
  102. REVERSE_MORSE.put("----","?");
  103. }
  104. }
  105.