Go to diff to previous submission
package javaapplication1; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Bugs { String line; int[] stack = new int[2000000]; while((line = reader.readLine()) != null) { int idx = -1; int state = 0; StringBuilder sb = new StringBuilder(); int c; while(n > 0 && (c = reader.read()) != -1) { if(c == '\n') { n--; sb = new StringBuilder(); } if(bug.charAt(state) == c) { state++; if(state == bug.length()) { if(idx >= 0) { state = stack[idx--]; } else { state = 0; } } } else if(bug.charAt(0) == c) { stack[++idx] = state; state = 1; } else { if(state > 0) { stack[++idx] = state; } for(int i = 0; i <= idx; i++) { state = stack[i]; sb.append(bug.substring(0, state)); } state = 0; idx = -1; sb.append((char) c); } } } } }
--- 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); } }