Source code for submission s496

Go to diff to previous submission

Fs.java

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7.  
  8.  
  9.  
  10. public class Fs {
  11.  
  12. private static Map<Character, String> morse = new HashMap<Character, String>();
  13.  
  14. private static void initializeMorse() {
  15. morse.put('A', ".-");
  16. morse.put('B', "-...");
  17. morse.put('C', "-.-.");
  18. morse.put('D', "-..");
  19. morse.put('E', ".");
  20. morse.put('F', "..-.");
  21. morse.put('G', "--.");
  22. morse.put('H', "....");
  23. morse.put('I', "..");
  24. morse.put('J', ".---");
  25. morse.put('K', "-.-");
  26. morse.put('L', ".-..");
  27. morse.put('M', "--");
  28. morse.put('N', "-.");
  29. morse.put('O', "---");
  30. morse.put('P', ".--.");
  31. morse.put('Q', "--.-");
  32. morse.put('R', ".-.");
  33. morse.put('S', "...");
  34. morse.put('T', "-");
  35. morse.put('U', "..-");
  36. morse.put('V', "...-");
  37. morse.put('W', ".--");
  38. morse.put('X', "-..-");
  39. morse.put('Y', "-.--");
  40. morse.put('Z', "--..");
  41. morse.put('_', "..--");
  42. morse.put(',', ".-.-");
  43. morse.put('.', "---.");
  44. morse.put('?', "----");
  45. }
  46.  
  47. public static void main(String[] args) throws FileNotFoundException {
  48.  
  49. initializeMorse();
  50.  
  51. //Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
  52. Scanner sc = new Scanner(new BufferedReader(new FileReader(new File("in.txt"))));
  53.  
  54. while (sc.hasNext()) {
  55.  
  56. String s = sc.next();
  57.  
  58. StringBuilder sb = new StringBuilder();
  59. ArrayList<Integer> numbers = new ArrayList<Integer>();
  60.  
  61. for (char c : s.toCharArray()) {
  62. String code = morse.get(c);
  63. sb.append(code);
  64. numbers.add(code.length());
  65. }
  66.  
  67. Collections.reverse(numbers);
  68. StringBuilder result = new StringBuilder();
  69.  
  70. int i = 0;
  71. for (int x : numbers) {
  72. String cut = sb.substring(i, i+x);
  73. result.append(findKey(cut));
  74. i += x;
  75. }
  76.  
  77. System.out.println(result);
  78.  
  79. }
  80.  
  81. }
  82.  
  83. private static char findKey(String cut) {
  84. for(Map.Entry<Character, String> en : morse.entrySet()) {
  85. if (en.getValue().equals(cut)) {
  86. return en.getKey();
  87. }
  88. }
  89.  
  90. }
  91. }
  92.  

Diff to submission s484

Fs.java

--- c5.s484.cteam054.fs.java.0.Fs.java
+++ c5.s496.cteam054.fs.java.0.Fs.java
@@ -1,3 +1,6 @@
 import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
 import java.io.InputStreamReader;
 import java.util.*;
@@ -42,13 +45,15 @@
         }
         
-        public static void main(String[] args) {        
+        public static void main(String[] args) throws FileNotFoundException {   
                 
                 initializeMorse();      
                 
-                Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
+                //Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
+                Scanner sc = new Scanner(new BufferedReader(new FileReader(new File("in.txt"))));
                 
-                while (sc.hasNextLine()) {
+                while (sc.hasNext()) {
                         
                         String s = sc.next();
+                        
                         StringBuilder sb = new StringBuilder();
                         ArrayList<Integer> numbers = new ArrayList<Integer>();
@@ -85,7 +90,3 @@
                 throw new IllegalArgumentException();
         }
-
-
-        
-
 }