Source code for submission s565

Fs.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. //package fo;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Scanner;
  10.  
  11. /**
  12.  *
  13.  * @author cteam017
  14.  */
  15. public class Fs {
  16.  
  17. public static void main(String[] args) {
  18. Scanner sc = new Scanner(System.in);
  19. ArrayList<Integer> lengths = new ArrayList<Integer>();
  20.  
  21. while (sc.hasNext()) {
  22. String line = sc.next();
  23.  
  24.  
  25. String morsedText = "";
  26. for (int i = 0; i < line.length(); i++) {
  27. String morsedChar = toMorse(line.charAt(i));
  28. int morsedLen = morsedChar.length();
  29.  
  30. lengths.add(morsedLen);
  31. morsedText += morsedChar;
  32. // System.out.println(line.charAt(i) + " -> " + morsedChar + " " + morsedLen);
  33. }
  34.  
  35. String lenStrs = "";
  36. for (Integer i : lengths){
  37. lenStrs+=i;
  38. }
  39. // System.out.println("Coded into: "+morsedText + " " + lenStrs);
  40.  
  41. Collections.reverse(lengths);
  42. String res = decodeMorse(morsedText, lengths);
  43.  
  44. System.out.println(res);
  45. lengths.clear();
  46. }
  47. }
  48.  
  49. static String decodeMorse(String in, ArrayList<Integer> lengths) {
  50. // System.out.println("\n\n\n\nDECODE MORSE: "+in);
  51. int pos = 0;
  52. String res = "";
  53.  
  54. for (int i = 0; i < lengths.size(); i++) {
  55. // System.out.println("new char, its len=" + lengths.get(i));
  56. String newChar = "";
  57. for (int k = 0; k < lengths.get(i); k++) {
  58. // System.out.println(" NewChar += " + in.charAt(pos) + " pos="+pos);
  59. newChar += in.charAt(pos++);
  60. }
  61. // System.out.println(" char completed: "+newChar);
  62. res+=fromMorse(newChar);
  63. // System.out.println("-- res so far: "+res);
  64. }
  65.  
  66. return res;
  67. }
  68.  
  69. static char fromMorse(String in) {
  70. if (in.equals(".-")) {
  71. return 'A';
  72. }
  73. if (in.equals("-...")) {
  74. return 'B';
  75. }
  76. if (in.equals("-.-.")) {
  77. return 'C';
  78. }
  79. if (in.equals("-..")) {
  80. return 'D';
  81. }
  82. if (in.equals(".")) {
  83. return 'E';
  84. }
  85. if (in.equals("..-.")) {
  86. return 'F';
  87. }
  88. if (in.equals("--.")) {
  89. return 'G';
  90. }
  91. if (in.equals("....")) {
  92. return 'H';
  93. }
  94. if (in.equals("..")) {
  95. return 'I';
  96. }
  97. if (in.equals(".---")) {
  98. return 'J';
  99. }
  100. if (in.equals("-.-")) {
  101. return 'K';
  102. }
  103. if (in.equals(".-..")) {
  104. return 'L';
  105. }
  106. if (in.equals("--")) {
  107. return 'M';
  108. }
  109. if (in.equals("-.")) {
  110. return 'N';
  111. }
  112. if (in.equals("---")) {
  113. return 'O';
  114. }
  115. if (in.equals(".--.")) {
  116. return 'P';
  117. }
  118. if (in.equals("--.-")) {
  119. return 'Q';
  120. }
  121. if (in.equals(".-.")) {
  122. return 'R';
  123. }
  124. if (in.equals("...")) {
  125. return 'S';
  126. }
  127. if (in.equals("-")) {
  128. return 'T';
  129. }
  130. if (in.equals("..-")) {
  131. return 'U';
  132. }
  133. if (in.equals("...-")) {
  134. return 'V';
  135. }
  136. if (in.equals(".--")) {
  137. return 'W';
  138. }
  139. if (in.equals("-..-")) {
  140. return 'X';
  141. }
  142. if (in.equals("-.--")) {
  143. return 'Y';
  144. }
  145. if (in.equals("--..")) {
  146. return 'Z';
  147. }
  148.  
  149. if (in.equals("..--")) {
  150. return '_';
  151. }
  152.  
  153. if (in.equals(".-.-")) {
  154. return ',';
  155. }
  156.  
  157. if (in.equals("---.")) {
  158. return '.';
  159. }
  160.  
  161. if (in.equals("----")) {
  162. return '?';
  163. }
  164.  
  165.  
  166. return '*';
  167.  
  168. }
  169.  
  170. static String toMorse(char letter) {
  171. switch (letter) {
  172. case 'A':
  173. return ".-";
  174. case 'B':
  175. return "-...";
  176. case 'C':
  177. return "-.-.";
  178. case 'D':
  179. return "-..";
  180. case 'E':
  181. return ".";
  182. case 'F':
  183. return "..-.";
  184. case 'G':
  185. return "--.";
  186. case 'H':
  187. return "....";
  188. case 'I':
  189. return "..";
  190. case 'J':
  191. return ".---";
  192. case 'K':
  193. return "-.-";
  194. case 'L':
  195. return ".-..";
  196. case 'M':
  197. return "--";
  198. case 'N':
  199. return "-.";
  200. case 'O':
  201. return "---";
  202. case 'P':
  203. return ".--.";
  204. case 'Q':
  205. return "--.-";
  206. case 'R':
  207. return ".-.";
  208. case 'S':
  209. return "...";
  210. case 'T':
  211. return "-";
  212. case 'U':
  213. return "..-";
  214. case 'V':
  215. return "...-";
  216. case 'W':
  217. return ".--";
  218. case 'X':
  219. return "-..-";
  220. case 'Y':
  221. return "-.--";
  222. case 'Z':
  223. return "--..";
  224.  
  225. case '_':
  226. return "..--";
  227. case ',':
  228. return ".-.-";
  229. case '.':
  230. return "---.";
  231. case '?':
  232. return "----";
  233. }
  234. return "";
  235. }
  236. }
  237.