Source code for submission s1293

Bugs.java

  1.  
  2. import java.io.BufferedReader;
  3.  
  4. /*
  5.  * To change this template, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.util.LinkedList;
  11. import java.util.StringTokenizer;
  12.  
  13.  
  14.  
  15. /**
  16.  *
  17.  * @author cteam022
  18.  */
  19. public class Bugs {
  20.  
  21.  
  22. /**
  23.   * @param args the command line arguments
  24.   */
  25. public static void main(String[] args) throws IOException {
  26. Bugs g = new Bugs();
  27. while(true){g.uloha();}
  28. }
  29.  
  30. void uloha() throws IOException{
  31. if(!input.ready()){System.exit(0);}
  32. st = new StringTokenizer(input.readLine());
  33.  
  34. int lines = Integer.valueOf(st.nextToken());
  35. char[] bug = st.nextToken().toCharArray();
  36. char bugFirst = bug[0];
  37. Item.bug = bug;
  38.  
  39. for (int i = 0; i < lines; i++) {
  40. char[] line = input.readLine().toCharArray();
  41. LinkedList<Item> lifo = new LinkedList<Item>();
  42. StringBuilder out = new StringBuilder();
  43.  
  44. for (int pos = 0; pos < line.length; pos++) {
  45. char current = line[pos];
  46.  
  47. if (current == bugFirst) {
  48. lifo.addLast(new Item(current));
  49. } else {
  50. if (lifo.isEmpty()) {
  51. out.append(current);
  52. } else {
  53. Item last = lifo.getLast();
  54.  
  55.  
  56. if (current == last.getNext()) {
  57. last.extend(current);
  58. if (last.isBug()) {
  59. lifo.removeLast();
  60. }
  61. } else {
  62. while (!lifo.isEmpty()) {
  63. out.append(lifo.pollFirst().text);
  64. }
  65. out.append(current);
  66. }
  67. }
  68. }
  69.  
  70. }
  71.  
  72. while (!lifo.isEmpty()) {
  73. out.append(lifo.pollFirst().text);
  74. }
  75.  
  76. System.out.println(out);
  77.  
  78. System.out.flush();
  79.  
  80. }
  81.  
  82.  
  83. }
  84.  
  85. static class Item {
  86. int length;
  87. char[] text;
  88. static char[] bug = new char[0];
  89.  
  90. Item(char first) {
  91. text = new char[bug.length];
  92. text[0] = first;
  93. length = 1;
  94. }
  95.  
  96. boolean isBug() {
  97. if (length != bug.length)
  98. return false;
  99.  
  100. return String.valueOf(text).equals(String.valueOf(bug));
  101. }
  102.  
  103. void extend(char next) {
  104. text[length++] = next;
  105. }
  106.  
  107. char getNext() {
  108. return bug[length];
  109. }
  110. }
  111.  
  112.  
  113. }
  114.