Source code for submission s534

FS.java

  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7.  
  8. /*
  9.  * To change this template, choose Tools | Templates
  10.  * and open the template in the editor.
  11.  */
  12.  
  13. /**
  14.  *
  15.  * @author cteam94
  16.  */
  17. public class FS {
  18. public static void main(String[] args) throws IOException {
  19.  
  20. Map<Character, String> map = new HashMap<Character, String>();
  21. map.put('A', ".-");
  22. map.put('B', "-...");
  23. map.put('C', "-.-.");
  24. map.put('D', "-..");
  25. map.put('E', ".");
  26. map.put('F', "..-.");
  27. map.put('G', "--.");
  28. map.put('H', "....");
  29. map.put('I', "..");
  30. map.put('J', ".---");
  31. map.put('K', "-.-");
  32. map.put('L', ".-..");
  33. map.put('M', "--");
  34. map.put('N', "-.");
  35. map.put('O', "---");
  36. map.put('P', ".--.");
  37. map.put('Q', "--.-");
  38. map.put('R', ".-.");
  39. map.put('S', "...");
  40. map.put('T', "-");
  41. map.put('U', "..-");
  42. map.put('V', "...-");
  43. map.put('W', ".--");
  44. map.put('X', "-..-");
  45. map.put('Y', "-.--");
  46. map.put('Z', "--..");
  47. map.put('_', "..--");
  48. map.put(',', ".-.-");
  49. map.put('.', "---.");
  50. map.put('?', "----");
  51.  
  52. Map<String, Character> map2 = new HashMap<String, Character>();
  53. for (Map.Entry<Character, String> entry : map.entrySet()) {
  54. Character c = entry.getKey();
  55. String s = entry.getValue();
  56.  
  57. map2.put(s, c);
  58.  
  59. }
  60.  
  61. while(readLine(br, map, map2));
  62. }
  63.  
  64. public static boolean readLine(BufferedReader br, Map<Character, String> map, Map<String, Character> map2) throws IOException {
  65. String orig = br.readLine();
  66. if (orig == null) return false;
  67.  
  68. StringBuilder a = new StringBuilder();
  69. Integer[] b = new Integer[orig.length()];
  70.  
  71. for (int i = 0; i < orig.length(); i++) {
  72. String temp = map.get(orig.charAt(i));
  73. a.append(temp);
  74. b[i] = temp.length();
  75. }
  76.  
  77. String s = a.toString();
  78. int counter = 0;
  79.  
  80. for (int i = b.length - 1; i >= 0; i--) {
  81. StringBuilder c = new StringBuilder();
  82. for (int j = 0; j < b[i]; j++) {
  83. c.append(s.charAt(counter));
  84. counter++;
  85. }
  86. System.out.print(map2.get(c.toString()));
  87. }
  88.  
  89. System.out.println();
  90.  
  91. return true;
  92. }
  93. }
  94.