Source code for submission s462

SenseOfSecurity.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package senseofsecuritz;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.InputStreamReader;
  9. import java.util.HashMap;
  10.  
  11. /**
  12.  *
  13.  * @author istenik3
  14.  */
  15. public class SenseOfSecurity {
  16. static private HashMap<String, Character> fromMorse = new HashMap<String, Character>();
  17. static private HashMap<Character, String> toMorse = new HashMap<Character, String>();
  18.  
  19. static public void map(Character c, String s) {
  20. fromMorse.put(s, c);
  21. toMorse.put(c, s);
  22. }
  23.  
  24. public static void main(String[] args) throws Exception {
  25. map('A', ".-");
  26. map('B', "-...");
  27. map('C', "-.-.");
  28. map('D', "-..");
  29. map('E', ".");
  30. map('F', "..-.");
  31. map('G', "--.");
  32. map('H', "....");
  33. map('I', "..");
  34. map('J', ".---");
  35. map('K', "-.-");
  36. map('L', ".-..");
  37. map('M', "--");
  38. map('N', "-.");
  39. map('O', "---");
  40. map('P', ".--.");
  41. map('Q', "--.-");
  42. map('R', ".-.");
  43. map('S', "...");
  44. map('T', "-");
  45. map('U', "..-");
  46. map('V', "...-");
  47. map('W', ".--");
  48. map('X', "-..-");
  49. map('Y', "-.--");
  50. map('Z', "--..");
  51. map('_', "..--");
  52. map(',', ".-.-");
  53. map('.', "---.");
  54. map('?', "----");
  55.  
  56.  
  57. String line = null;
  58. while((line = reader.readLine()) != null) {
  59. StringBuilder morse = new StringBuilder();
  60. StringBuilder integers = new StringBuilder();
  61.  
  62. for(char c : line.toCharArray()) {
  63. morse.append(toMorse.get(c));
  64. integers.append(toMorse.get(c).length());
  65. }
  66.  
  67. int offset = 0;
  68. for(int i=integers.length()-1; i>=0; i--) {
  69. int l = Integer.parseInt(""+integers.charAt(i));
  70. String m = morse.substring(offset, offset+l);
  71. offset += l;
  72.  
  73. System.out.print(fromMorse.get(m));
  74. }
  75.  
  76. System.out.println();
  77. }
  78. }
  79. }
  80.