Go to diff to previous submission
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Bugs { static public int replace(int offset) { int hladanyIndex = 0; int hladanyStart = -1; for(int i=offset; i < co.length; i++) { char c = co[i]; if(c == cim[hladanyIndex]) { if(hladanyIndex == 0) { hladanyStart = i; } hladanyIndex++; if(hladanyIndex == cim.length) { return i+1; } } else { if(c == cim[0]) { int result = replace(i); if(result < 0) { return -(i+1); } else { i = result-1; } } else { return -(i+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++) { } i = -result-1; } else { i = result-1; } } else { } } /*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 > 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; } }*/ } static public char[] co; public static char[] cim; String identifier; while((identifier = reader.readLine()) != null) { int medzera = identifier.indexOf(" "); char[] hladame = identifier.substring(medzera+1).toCharArray(); for(int i=0; i<riadkov; ++i) { char [] line = reader.readLine().toCharArray(); co = line; cim = hladame; replaceTop(); } } } }
--- 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;