Source code for submission s695

Bugs.java

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