public class KMPProcess { public static void buildNext(String str, int[] next) { if (str == null || next == null || str.length() != next.length) throw new IllegalArgumentException();
int i = 0; int k;
next[i] = k = -1;
while (i < str.length() - 1) { if (k == -1 || str.charAt(i) == str.charAt(k)) { i ++; k ++; next[i] = k; } else { k = next[k]; } } }