import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Stringy {
public static void najdiVyskyty(String retazec, String vzorka) {
int poz = 0;
while (poz <= retazec.length() - vzorka.length()) {
int i = 0;
while (vzorka.charAt(i) == retazec.charAt(poz + i)) {
i++;
if (i == vzorka.length()) {
System.out.println(poz);
break;
}
}
poz++;
}
}
public static int prefixovySufix(String s) {
int najvacsi = 0;
for (int i = 1; i <= s.length() - 1; i++) {
int j = 0;
while (s.charAt(j) == s.charAt(s.length() + j - i)) {
j++;
if (j == i) {
//System.out.println("mam " + i);
najvacsi = i;
break;
}
}
}
return najvacsi;
}
private static int[][] kmpTabulka(String vzorka) {
Set<Character> pismenka = new HashSet<Character>();
for (int i = 0; i < vzorka.length(); i++) {
pismenka.add(vzorka.charAt(i));
}
pismenka.add('→');
Character[] riadkyVTabulke = new Character[pismenka.size()];
int k = 0;
for (Character pismenko : pismenka) {
riadkyVTabulke[k] = pismenko;
k++;
}
System.out.println(Arrays.toString(riadkyVTabulke));
int[][] tabulka = new int[pismenka.size()][vzorka.length()];
for (int i = 0; i < tabulka.length; i++) {
for (int j = 0; j < tabulka[0].length; j++) {
if (riadkyVTabulke[i] == vzorka.charAt(j)) {
tabulka[i][j] = 0;
} else {
tabulka[i][j] = j
+ 1
- prefixovySufix(vzorka.substring(0, j)
+ riadkyVTabulke[i]);
}
}
}
return tabulka;
}
public static void main(String[] args) {
// Stringy s = new Stringy();
// s.najdiVyskyty("cdbabadaba", "ab");
//prefixovySufix("aab");
int[][] tabulka = kmpTabulka("XYXYZ");
for (int i = 0; i < tabulka.length; i++) {
System.out.println(Arrays.toString(tabulka[i]));
}
}
}