Source code for submission s549

Fs.java

  1. /*
  2.  * CTU Open 2013
  3.  * HES
  4.  */
  5. package template;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.InputStreamReader;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Stack;
  12. import java.util.StringTokenizer;
  13.  
  14. /**
  15.  *
  16.  * @author cteam007
  17.  */
  18. public class Fs {
  19.  
  20.  
  21. void useLine(String s) {
  22. st = new StringTokenizer(s);
  23. }
  24.  
  25. String nextToken() throws Exception {
  26. while (!st.hasMoreTokens()) {
  27. st = new StringTokenizer(input.readLine());
  28. }
  29. return st.nextToken();
  30. }
  31.  
  32. int nextInt() throws Exception {
  33. return Integer.parseInt(nextToken());
  34. }
  35. static Map<String, String> morseMap;
  36.  
  37. /**
  38.   * @param args the command line arguments
  39.   */
  40. public static void main(String[] args) throws Exception {
  41.  
  42. morseMap = new HashMap<String, String>();
  43.  
  44. morseMap.put(".-", "A");
  45. morseMap.put("-...", "B");
  46. morseMap.put("-.-.", "C");
  47. morseMap.put("-..", "D");
  48. morseMap.put(".", "E");
  49. morseMap.put("..-.", "F");
  50. morseMap.put("--.", "G");
  51. morseMap.put("....", "H");
  52. morseMap.put("..", "I");
  53. morseMap.put(".---", "J");
  54. morseMap.put("-.-", "K");
  55. morseMap.put(".-..", "L");
  56. morseMap.put("--", "M");
  57. morseMap.put("-.", "N");
  58. morseMap.put("---", "O");
  59. morseMap.put(".--.", "P");
  60. morseMap.put("--.-", "Q");
  61. morseMap.put(".-.", "R");
  62. morseMap.put("...", "S");
  63. morseMap.put("-", "T");
  64. morseMap.put("..-", "U");
  65. morseMap.put("...-", "V");
  66. morseMap.put(".--", "W");
  67. morseMap.put("-..-", "X");
  68. morseMap.put("-.--", "Y");
  69. morseMap.put("--..", "Z");
  70. morseMap.put("..--", "_");
  71. morseMap.put(".-.-", ",");
  72. morseMap.put("---.", ".");
  73. morseMap.put("----", "?");
  74.  
  75.  
  76. Fs instance = new Fs();
  77. while (instance.run()) {
  78. }
  79. }
  80.  
  81. boolean run() throws Exception {
  82. String in = input.readLine();
  83. if (in == null) {
  84. return false;
  85. }
  86.  
  87. useLine(in);
  88.  
  89. String cryptedString = nextToken();
  90.  
  91. Stack<Integer> intStack = toStack(cryptedString);
  92. String cryptedMorse = toMorse(cryptedString);
  93.  
  94. int offset = 0;
  95. while (!intStack.empty()) {
  96. int len = intStack.pop();
  97. System.out.print(decodeMorse(parseMorse(cryptedMorse, offset, len)));
  98. offset += len;
  99. }
  100. System.out.println("");
  101.  
  102. return true;
  103. }
  104.  
  105.  
  106. Stack<Integer> toStack(String cryptedText) {
  107. Stack<Integer> stack = new Stack<Integer>();
  108.  
  109. int count = cryptedText.length();
  110.  
  111. for (int i = 0; i < count; i++) {
  112. stack.add(morse(cryptedText.charAt(i)).length());
  113. }
  114.  
  115. return stack;
  116. }
  117.  
  118. String toMorse(String cryptedText) {
  119. String morseT = "";
  120.  
  121. int count = cryptedText.length();
  122.  
  123. for (int i = 0; i < count; i++) {
  124. morseT += morse(cryptedText.charAt(i));
  125. }
  126.  
  127. return morseT;
  128. }
  129.  
  130. String parseMorse(String cryptedText, int offset, int length) {
  131. return cryptedText.substring(offset, offset + length);
  132. }
  133.  
  134. String morse(char c) {
  135. switch (c) {
  136. case 'A':
  137. return ".-";
  138. case 'B':
  139. return "-...";
  140. case 'C':
  141. return "-.-.";
  142. case 'D':
  143. return "-..";
  144. case 'E':
  145. return ".";
  146. case 'F':
  147. return "..-.";
  148. case 'G':
  149. return "--.";
  150. case 'H':
  151. return "....";
  152. case 'I':
  153. return "..";
  154. case 'J':
  155. return ".---";
  156. case 'K':
  157. return "-.-";
  158. case 'L':
  159. return ".-..";
  160. case 'M':
  161. return "--";
  162. case 'N':
  163. return "-.";
  164. case 'O':
  165. return "---";
  166. case 'P':
  167. return ".--.";
  168. case 'Q':
  169. return "--.-";
  170. case 'R':
  171. return ".-.";
  172. case 'S':
  173. return "...";
  174. case 'T':
  175. return "-";
  176. case 'U':
  177. return "..-";
  178. case 'V':
  179. return "...-";
  180. case 'W':
  181. return ".--";
  182. case 'X':
  183. return "-..-";
  184. case 'Y':
  185. return "-.--";
  186. case 'Z':
  187. return "--..";
  188. case '_':
  189. return "..--";
  190. case ',':
  191. return ".-.-";
  192. case '.':
  193. return "---.";
  194. case '?':
  195. return "----";
  196. }
  197. return "";
  198. }
  199.  
  200. String decodeMorse(String morse) {
  201. return morseMap.get(morse);
  202. }
  203. }
  204.