Source code for submission s568

Fs.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. import java.util.*;
  7. import java.util.Map;
  8.  
  9. /**
  10.  *
  11.  * @author cteam92
  12.  */
  13. public class Fs {
  14. /**
  15.   * @param args the command line arguments
  16.   */
  17. public static void main(String[] args) {
  18. Map<String, String> toMorse = new HashMap<String, String>();
  19. Map<String, String> fromMorse = new HashMap<String, String>();
  20.  
  21. toMorse.put("A", ".-");
  22. toMorse.put("B", "-...");
  23. toMorse.put("C", "-.-.");
  24. toMorse.put("D", "-..");
  25. toMorse.put("E", ".");
  26. toMorse.put("F", "..-.");
  27. toMorse.put("G", "--.");
  28. toMorse.put("H", "....");
  29. toMorse.put("I", "..");
  30. toMorse.put("J", ".---");
  31. toMorse.put("K", "-.-");
  32. toMorse.put("L", ".-..");
  33. toMorse.put("M", "--");
  34. toMorse.put("N", "-.");
  35. toMorse.put("O", "---");
  36. toMorse.put("P", ".--.");
  37. toMorse.put("Q", "--.-");
  38. toMorse.put("R", ".-.");
  39. toMorse.put("S", "...");
  40. toMorse.put("T", "-");
  41. toMorse.put("U", "..-");
  42. toMorse.put("V", "...-");
  43. toMorse.put("W", ".--");
  44. toMorse.put("X", "-..-");
  45. toMorse.put("Y", "-.--");
  46. toMorse.put("Z", "--..");
  47. toMorse.put("_", "..--");
  48. toMorse.put(",", ".-.-");
  49. toMorse.put(".", "---.");
  50. toMorse.put("?", "----");
  51.  
  52. fromMorse.put(".-", "A");
  53. fromMorse.put("-...", "B");
  54. fromMorse.put("-.-.", "C");
  55. fromMorse.put("-..", "D");
  56. fromMorse.put(".", "E");
  57. fromMorse.put("..-.", "F");
  58. fromMorse.put("--.", "G");
  59. fromMorse.put("....", "H");
  60. fromMorse.put("..", "I");
  61. fromMorse.put(".---", "J");
  62. fromMorse.put("-.-", "K");
  63. fromMorse.put(".-..", "L");
  64. fromMorse.put("--", "M");
  65. fromMorse.put("-.", "N");
  66. fromMorse.put("---", "O");
  67. fromMorse.put(".--.", "P");
  68. fromMorse.put("--.-", "Q");
  69. fromMorse.put(".-.", "R");
  70. fromMorse.put("...", "S");
  71. fromMorse.put("-", "T");
  72. fromMorse.put("..-", "U");
  73. fromMorse.put("...-", "V");
  74. fromMorse.put(".--", "W");
  75. fromMorse.put("-..-", "X");
  76. fromMorse.put("-.--", "Y");
  77. fromMorse.put("--..", "Z");
  78. fromMorse.put("..--", "_");
  79. fromMorse.put(".-.-", ",");
  80. fromMorse.put("---.", ".");
  81. fromMorse.put("----", "?");
  82.  
  83. Scanner sc = new Scanner(System.in);
  84. while (true) {
  85. if (!sc.hasNext()) break;
  86.  
  87. // get numbers
  88. String input = sc.nextLine();
  89. String morseCode = "";
  90. String numbers = "";
  91. for (int i = 0; i < input.length(); i++) {
  92. String thisIndex = input.substring(i, i + 1);
  93. morseCode += toMorse.get(thisIndex);
  94. numbers = toMorse.get(thisIndex).length() + numbers;
  95. }
  96.  
  97. // decode by reversed numbers
  98. int pointer = 0;
  99. for (int i = 0; i < numbers.length(); i++) {
  100. int thisLen = Integer.parseInt(numbers.substring(i, i + 1));
  101. String toDecode = morseCode.substring(pointer, pointer + thisLen);
  102. System.out.print(fromMorse.get(toDecode));
  103.  
  104. pointer += thisLen;
  105. }
  106. System.out.println();
  107. }
  108. }
  109. }
  110.