Can someone please explain this piece of code because it just doesn’t make sense to me.
vector<int> kmpNext(const string& x) {
int m = x.size();
vector<int> kmpNext(m, 0);
kmpNext[0] = -1;
int i = 0, j = -1;
while (i < m - 1) {
while (j >= 0 && x[i] != x[j]) j = kmpNext[j];
i++;
j++;
kmpNext[i] = (x[i] == x[j]) ? kmpNext[j] : j;
}
return kmpNext;
}
For example, if I have the word: rokyroy, I should get the kmpNext table to be: -1 0 0 0 0 1 2, but according to this code(at least how I understand it), I get: -1 0 0 0 -1 0 0, which I think is incorrect. Thanks in advance.