Source code for submission s575

Fs.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6.  
  7. /**
  8.  *
  9.  * @author cteam041
  10.  */
  11. import java.util.Scanner;
  12. import java.util.HashMap;
  13. public class Fs {
  14.  
  15. /**
  16.   * @param args the command line arguments
  17.   */
  18. static String decode(String cipher,HashMap<String,String> table,HashMap<String,String> table2) {
  19. String decoded="";
  20. String morse="";
  21. int counter=0;
  22. int[] chars=new int[cipher.length()];
  23. for(int i=0;i<cipher.length();i++) {
  24. String s = "";
  25. s+=cipher.charAt(i);
  26. morse+=table.get(s);
  27. chars[i]=table.get(s).length();
  28. }
  29. for(int i=chars.length-1;i>=0;i--) {
  30. decoded+=table2.get(morse.substring(counter, counter+chars[i]));
  31. counter+=chars[i];
  32. }
  33. return decoded;
  34. }
  35. public static void main(String[] args) {
  36. HashMap<String,String> charToCode=new HashMap<String,String>();
  37. charToCode.put("A", ".-");
  38. charToCode.put("B", "-...");
  39. charToCode.put("C", "-.-.");
  40. charToCode.put("D", "-..");
  41. charToCode.put("E", ".");
  42. charToCode.put("F", "..-.");
  43. charToCode.put("G", "--.");
  44. charToCode.put("H", "....");
  45. charToCode.put("I", "..");
  46. charToCode.put("J", ".---");
  47. charToCode.put("K", "-.-");
  48. charToCode.put("L", ".-..");
  49. charToCode.put("M", "--");
  50. charToCode.put("N", "-.");
  51. charToCode.put("O", "---");
  52. charToCode.put("P", ".--.");
  53. charToCode.put("Q", "--.-");
  54. charToCode.put("R", ".-.");
  55. charToCode.put("S", "...");
  56. charToCode.put("T", "-");
  57. charToCode.put("U", "..-");
  58. charToCode.put("V", "...-");
  59. charToCode.put("W", ".--");
  60. charToCode.put("X", "-..-");
  61. charToCode.put("Y", "-.--");
  62. charToCode.put("Z", "--..");
  63. charToCode.put("_", "..--");
  64. charToCode.put(",", ".-.-");
  65. charToCode.put(".", "---.");
  66. charToCode.put("?", "----");
  67. HashMap<String,String> charToCode2=new HashMap<String,String>();
  68. charToCode2.put(".-","A");
  69. charToCode2.put("-...","B");
  70. charToCode2.put("-.-.","C");
  71. charToCode2.put("-..","D");
  72. charToCode2.put(".","E");
  73. charToCode2.put("..-.","F");
  74. charToCode2.put("--.","G");
  75. charToCode2.put("....","H");
  76. charToCode2.put("..","I");
  77. charToCode2.put(".---","J");
  78. charToCode2.put("-.-","K");
  79. charToCode2.put(".-..","L");
  80. charToCode2.put("--","M");
  81. charToCode2.put("-.","N");
  82. charToCode2.put("---","O");
  83. charToCode2.put(".--.","P");
  84. charToCode2.put("--.-","Q");
  85. charToCode2.put(".-.","R");
  86. charToCode2.put("...","S");
  87. charToCode2.put("-","T");
  88. charToCode2.put("..-","U");
  89. charToCode2.put("...-","V");
  90. charToCode2.put(".--","W");
  91. charToCode2.put("-..-","X");
  92. charToCode2.put("-.--","Y");
  93. charToCode2.put("--..","Z");
  94. charToCode2.put("..--","_");
  95. charToCode2.put(".-.-",",");
  96. charToCode2.put("---.",".");
  97. charToCode2.put("----","?");
  98. Scanner sc=new Scanner(System.in);
  99. while(true) {
  100. try{
  101. String cipher=sc.nextLine();
  102. System.out.println(decode(cipher,charToCode,charToCode2));
  103. } catch(Exception e) {
  104. break;
  105. }
  106. }
  107.  
  108.  
  109. // TODO code application logic here
  110. }
  111.  
  112. }
  113.