Source code for submission s467

Main.java

  1.  
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.util.StringTokenizer;
  5.  
  6. public class Main {
  7.  
  8. protected final int TABLE_LENGTH = 30;
  9. protected String table[][] = new String[TABLE_LENGTH][2];
  10. protected int tableLength[] = new int[TABLE_LENGTH];
  11.  
  12. public static void main(String[] args) throws Exception {
  13. Main p = new Main();
  14. p.run();
  15. }
  16.  
  17. protected void run() throws Exception {
  18. initTable();
  19. while (input.ready()) {
  20. String line = nextLine();
  21. int[] lengths = new int[line.length()];
  22. for (int i = 0; i < line.length(); i++) {
  23. int index = getTableIndex(line.charAt(i));
  24. buf.append(table[index][1]);
  25. lengths[i] = tableLength[index];
  26. }
  27. String morse = buf.toString();
  28. int beggin = 0;
  29. for (int i = line.length() - 1; i >= 0; i--) {
  30. res.append(getCharacter(morse.substring(beggin, beggin + lengths[i])));
  31. beggin += lengths[i];
  32. }
  33. System.out.println(res.toString());
  34. }
  35. }
  36.  
  37. protected int getTableIndex(char match) {
  38. for (int i = 0; i < TABLE_LENGTH; i++) {
  39. if (table[i][0].charAt(0) == match) {
  40. return i;
  41. }
  42. }
  43. return 0;
  44. }
  45.  
  46. protected String getCharacter(String match) {
  47. for (int i = 0; i < TABLE_LENGTH; i++) {
  48. if (table[i][1].equals(match)) {
  49. return table[i][0];
  50. }
  51. }
  52. return "A";
  53. }
  54.  
  55. protected void initTable() {
  56. table[0][0] = "A"; table[0][1] = ".-";
  57. table[1][0] = "B"; table[1][1] = "-...";
  58. table[2][0] = "C"; table[2][1] = "-.-.";
  59. table[3][0] = "D"; table[3][1] = "-..";
  60. table[4][0] = "E"; table[4][1] = ".";
  61. table[5][0] = "F"; table[5][1] = "..-.";
  62. table[6][0] = "G"; table[6][1] = "--.";
  63. table[7][0] = "H"; table[7][1] = "....";
  64. table[8][0] = "I"; table[8][1] = "..";
  65. table[9][0] = "J"; table[9][1] = ".---";
  66. table[10][0] = "K"; table[10][1] = "-.-";
  67. table[11][0] = "L"; table[11][1] = ".-..";
  68. table[12][0] = "M"; table[12][1] = "--";
  69. table[13][0] = "N"; table[13][1] = "-.";
  70. table[14][0] = "O"; table[14][1] = "---";
  71. table[15][0] = "P"; table[15][1] = ".--.";
  72. table[16][0] = "Q"; table[16][1] = "--.-";
  73. table[17][0] = "R"; table[17][1] = ".-.";
  74. table[18][0] = "S"; table[18][1] = "...";
  75. table[19][0] = "T"; table[19][1] = "-";
  76. table[20][0] = "U"; table[20][1] = "..-";
  77. table[21][0] = "V"; table[21][1] = "...-";
  78. table[22][0] = "W"; table[22][1] = ".--";
  79. table[23][0] = "X"; table[23][1] = "-..-";
  80. table[24][0] = "Y"; table[24][1] = "-.--";
  81. table[25][0] = "Z"; table[25][1] = "--..";
  82. table[26][0] = "_"; table[26][1] = "..--";
  83. table[27][0] = ","; table[27][1] = ".-.-";
  84. table[28][0] = "."; table[28][1] = "---.";
  85. table[29][0] = "?"; table[29][1] = "----";
  86.  
  87. for (int i = 0; i < TABLE_LENGTH; i++) {
  88. tableLength[i] = table[i][1].length();
  89. }
  90. }
  91.  
  92.  
  93. public String nextToken() throws Exception {
  94. while (!st.hasMoreTokens()) {
  95. st = new StringTokenizer(input.readLine());
  96. }
  97. return st.nextToken();
  98. }
  99.  
  100. public int nextInt() throws Exception {
  101. return Integer.parseInt(nextToken());
  102. }
  103.  
  104. public String nextLine() throws Exception {
  105. return input.readLine();
  106. }
  107.  
  108. }
  109.