Source code for submission s1305

Go to diff to previous submission

Bugs.java

  1.  
  2.  
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.util.Scanner;
  8.  
  9. public class Bugs {
  10. static public int replace(int offset) {
  11. int hladanyIndex = 0;
  12.  
  13. for(int i=offset; i < co.length; i++) {
  14. char c = co[i];
  15.  
  16. if(c == cim[hladanyIndex]) {
  17. hladanyIndex++;
  18. if(hladanyIndex == cim.length) {
  19. return i+1;
  20. }
  21. } else {
  22. if(c == cim[0]) {
  23. int result = replace(i);
  24. if(result < 0) {
  25. return -(i+1);
  26. } else {
  27. i = result-1;
  28. }
  29. } else {
  30. return -(i+1);
  31. }
  32. }
  33. }
  34.  
  35. return -co.length;
  36. }
  37.  
  38. static public void replaceTop() {
  39. for(int i=0; i < co.length; i++) {
  40. if(co[i] == cim[0]) {
  41. int result = replace(i);
  42. if(result < 0) {
  43. for(int j=i; j<-result; j++) {
  44. System.out.print(co[j]);
  45. }
  46. i = -result-1;
  47. } else {
  48. i = result-1;
  49. }
  50. } else {
  51. System.out.print(co[i]);
  52. }
  53. }
  54.  
  55. /*int lastPrinted = 0;
  56. for(int i=0; i < co.length; i++) {
  57. char c = co[i];
  58.  
  59. if(c == cim[0]) {
  60. int result = replace(i);
  61. if(result > 0) {
  62. i = result-1;
  63. lastPrinted = i+1;
  64. continue;
  65. } else {
  66. i = -result;
  67.  
  68. for(int j=lastPrinted+1; j <= i; j++) {
  69. System.out.print(co[j]);
  70. }
  71. }
  72. } else {
  73. System.out.print(c);
  74. lastPrinted = i;
  75. }
  76.  
  77.  
  78. }*/
  79. }
  80.  
  81. static public char[] co;
  82. public static char[] cim;
  83.  
  84. public static void main(String[] args) throws IOException {
  85. Scanner scanner = new Scanner(System.in);
  86. while(scanner.hasNextInt()) {
  87. int riadkov = scanner.nextInt();
  88. scanner.skip(" ");
  89. char [] hladame = scanner.nextLine().toCharArray();
  90.  
  91. for(int i=0; i<riadkov; ++i) {
  92. char [] line = scanner.nextLine().toCharArray();
  93.  
  94. co = line;
  95. cim = hladame;
  96.  
  97. replaceTop();
  98.  
  99. System.out.println();
  100. }
  101. }
  102.  
  103. /*BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  104.  
  105.   String identifier;
  106.  
  107.   while((identifier = reader.readLine()) != null) {
  108.   int medzera = identifier.indexOf(" ");
  109.   int riadkov = Integer.valueOf(identifier.substring(0, medzera));
  110.   char[] hladame = identifier.substring(medzera+1).toCharArray();
  111.  
  112.   for(int i=0; i<riadkov; ++i) {
  113. char [] line = reader.readLine().toCharArray();
  114.  
  115. co = line;
  116. cim = hladame;
  117.  
  118. replaceTop();
  119.  
  120.   System.out.println();
  121.   }
  122.   }*/
  123. }
  124. }
  125.  

Diff to submission s1269

Bugs.java

--- c4.s1269.cteam121.bugs.java.0.Bugs.java
+++ c4.s1305.cteam121.bugs.java.0.Bugs.java
@@ -5,9 +5,9 @@
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.util.Scanner;
 
 public class Bugs {
     static public int replace(int offset) {
         int hladanyIndex = 0;
-        int hladanyStart = -1;
         
         for(int i=offset; i < co.length; i++) {
@@ -15,7 +15,4 @@
             
             if(c == cim[hladanyIndex]) {
-                if(hladanyIndex == 0) {
-                    hladanyStart = i;
-                }
                 hladanyIndex++;
                 if(hladanyIndex == cim.length) {
@@ -86,5 +83,23 @@
     
     public static void main(String[] args) throws IOException {
-        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
+        Scanner scanner = new Scanner(System.in);
+        while(scanner.hasNextInt()) {
+            int riadkov = scanner.nextInt();
+            scanner.skip(" ");
+            char [] hladame = scanner.nextLine().toCharArray();
+            
+            for(int i=0; i<riadkov; ++i) {
+                char [] line = scanner.nextLine().toCharArray();
+                
+                co = line;
+                cim = hladame;
+                
+                replaceTop();
+                
+                System.out.println();
+            }
+        }
+        
+        /*BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
         
         String identifier;
@@ -105,5 +120,5 @@
                 System.out.println();
             }
-        }
+        }*/
     }
 }