Source code for submission s477

Fs.java

  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Fs
  5. {
  6. public static HashMap<Character, String> alphabet = new HashMap<Character, String>();
  7. public static HashMap<String, Character> reverse_alphabet = new HashMap<String, Character>();
  8.  
  9. public static void main(String[] args) throws IOException {
  10.  
  11. alphabet.put('A', ".-");
  12. alphabet.put('B', "-...");
  13. alphabet.put('C', "-.-.");
  14. alphabet.put('D', "-..");
  15. alphabet.put('E', ".");
  16. alphabet.put('F', "..-.");
  17. alphabet.put('G', "--.");
  18. alphabet.put('H', "....");
  19. alphabet.put('I', "..");
  20. alphabet.put('J', ".---");
  21. alphabet.put('K', "-.-");
  22. alphabet.put('L', ".-..");
  23. alphabet.put('M', "--");
  24. alphabet.put('N', "-.");
  25. alphabet.put('O', "---");
  26. alphabet.put('P', ".--.");
  27. alphabet.put('Q', "--.-");
  28. alphabet.put('R', ".-.");
  29. alphabet.put('S', "...");
  30. alphabet.put('T', "-");
  31. alphabet.put('U', "..-");
  32. alphabet.put('V', "...-");
  33. alphabet.put('W', ".--");
  34. alphabet.put('X', "-..-");
  35. alphabet.put('Y', "-.--");
  36. alphabet.put('Z', "--..");
  37. alphabet.put('_', "..--");
  38. alphabet.put(',', ".-.-");
  39. alphabet.put('.', "---.");
  40. alphabet.put('?', "----");
  41.  
  42. for(Character c : alphabet.keySet()) {
  43. reverse_alphabet.put(alphabet.get(c), c);
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. while(br.ready()) {
  53. String line = br.readLine();
  54. one(line);
  55.  
  56. }
  57.  
  58. }
  59.  
  60. public static void one(String line) {
  61. char[] cr = line.toCharArray();
  62. String[] converted = new String[line.length()];
  63. String converted_line = "";
  64. //int[] lengths = new int[line.length()];
  65. ArrayList<Integer> lengths = new ArrayList<Integer>();
  66.  
  67. for (int i = 0; i < cr.length; i++) {
  68. converted[i] = alphabet.get(cr[i]);
  69. converted_line = converted_line + converted[i];
  70. lengths.add(converted[i].length());
  71. }
  72.  
  73. Collections.reverse(lengths);
  74.  
  75. int position = 0;
  76. String code = "";
  77.  
  78. for(Integer i : lengths) {
  79. code = converted_line.substring(position, position + i);
  80. position = position + i;
  81. //System.out.print(code+" ");
  82. System.out.print(reverse_alphabet.get(code));
  83. }
  84.  
  85. System.out.println("");
  86.  
  87. }
  88.  
  89.  
  90. }