Source code for submission s724

Go to diff to previous submission

Bugs.java

  1.  
  2.  
  3. package javaapplication1;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8.  
  9. public class Bugs {
  10.  
  11. public static void main(String[] args) throws IOException {
  12. String line;
  13.  
  14. int[] stack = new int[2000000];
  15.  
  16. while((line = reader.readLine()) != null) {
  17. String[] split = line.split(" ");
  18.  
  19. int n = Integer.parseInt(split[0]);
  20. String bug = split[1];
  21.  
  22. int idx = -1;
  23. int state = 0;
  24.  
  25. StringBuilder sb = new StringBuilder();
  26.  
  27. int c;
  28. while(n > 0 && (c = reader.read()) != -1) {
  29. if(c == '\n') {
  30. n--;
  31. sb = new StringBuilder();
  32. }
  33.  
  34. if(bug.charAt(state) == c) {
  35. state++;
  36. if(state == bug.length()) {
  37. if(idx >= 0) {
  38. state = stack[idx--];
  39. } else {
  40. state = 0;
  41. }
  42. }
  43. } else if(bug.charAt(0) == c) {
  44. stack[++idx] = state;
  45. state = 1;
  46. } else {
  47. if(state > 0) {
  48. stack[++idx] = state;
  49. }
  50. for(int i = 0; i <= idx; i++) {
  51. state = stack[i];
  52. sb.append(bug.substring(0, state));
  53. }
  54. state = 0;
  55. idx = -1;
  56. sb.append((char) c);
  57. }
  58. }
  59. }
  60. }
  61.  
  62. }
  63.  

Diff to submission s695

Bugs.java

--- c4.s695.cteam026.bugs.java.0.Bugs.java
+++ c4.s724.cteam026.bugs.java.0.Bugs.java
@@ -10,26 +12,29 @@
         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
         String line;
-
+        
         int[] stack = new int[2000000];
-
-        while ((line = reader.readLine()) != null) {
+        
+        while((line = reader.readLine()) != null) {
             String[] split = line.split(" ");
-
+            
             int n = Integer.parseInt(split[0]);
-            char[] bug = split[1].toCharArray();
-
+            String bug = split[1];
+            
             int idx = -1;
             int state = 0;
-
+            
+            StringBuilder sb = new StringBuilder();
+            
             int c;
-            while (n > 0 && (c = reader.read()) != -1) {
-                if (c == '\n') {
+            while(n > 0 && (c = reader.read()) != -1) {
+                if(c == '\n') {
                     n--;
+                    sb = new StringBuilder();
                 }
-
-                if (bug[state] == c) {
+                
+                if(bug.charAt(state) == c) {
                     state++;
-                    if (state == bug.length) {
-                        if (idx >= 0) {
+                    if(state == bug.length()) {
+                        if(idx >= 0) {
                             state = stack[idx--];
                         } else {
@@ -37,5 +42,5 @@
                         }
                     }
-                } else if (bug[0] == c) {
+                } else if(bug.charAt(0) == c) {
                     stack[++idx] = state;
                     state = 1;
@@ -46,11 +51,9 @@
                     for(int i = 0; i <= idx; i++) {
                         state = stack[i];
-                        for (int j = 0; j < state; j++) {
-                            System.out.print(bug[j]);
-                        }
+                        sb.append(bug.substring(0, state));
                     }
                     state = 0;
                     idx = -1;
-                    System.out.print((char) c);
+                    sb.append((char) c);
                 }
             }