Source code for submission s1269

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

Diff to submission s1096

Bugs.java

--- c4.s1096.cteam121.bugs.java.0.Bugs.java
+++ c4.s1269.cteam121.bugs.java.0.Bugs.java
@@ -5,5 +5,4 @@
 import java.io.IOException;
 import java.io.InputStreamReader;
-import java.util.LinkedList;
 
 public class Bugs {
@@ -21,42 +20,68 @@
                 hladanyIndex++;
                 if(hladanyIndex == cim.length) {
-                    return i-1;
+                    return i+1;
                 }
             } else {
                 if(c == cim[0]) {
                     int result = replace(i);
-                    if(result == -1) {
-                        return -1;
+                    if(result < 0) {
+                        return -(i+1);
                     } else {
-                        i = result+1;
+                        i = result-1;
                     }
                 } else {
-                    return -1;
+                    return -(i+1);
                 }
             }
         }
         
-        return -1;
+        return -co.length;
     }
     
     static public void replaceTop() {
         for(int i=0; i < co.length; i++) {
+            if(co[i] == cim[0]) {
+                int result = replace(i);
+                if(result < 0) {
+                    for(int j=i; j<-result; j++) {
+                        System.out.print(co[j]);
+                    }
+                    i = -result-1;
+                } else {
+                    i = result-1;
+                }
+            } else {
+                System.out.print(co[i]);
+            }
+        }
+        
+        /*int lastPrinted = 0;
+        for(int i=0; i < co.length; i++) {
             char c = co[i];
             
             if(c == cim[0]) {
                 int result = replace(i);
-                if(result != -1) {
-                    i = result+1;
+                if(result > 0) {
+                    i = result-1;
+                    lastPrinted = i+1;
                     continue;
+                } else {
+                    i = -result;
+                    
+                    for(int j=lastPrinted+1; j <= i; j++) {
+                        System.out.print(co[j]);
+                    }
                 }
+            } else {
+                System.out.print(c);
+                lastPrinted = i;
             }
             
-            System.out.print(c);
-        }
+
+        }*/
     }
     
     static public char[] co;
     public static char[] cim;
-    static public LinkedList<int[]> intervaly;
     
     public static void main(String[] args) throws IOException {
@@ -72,5 +97,4 @@
             for(int i=0; i<riadkov; ++i) {
                 char [] line = reader.readLine().toCharArray();
-                intervaly = new LinkedList<int[]>();
                 
                 co = line;