字符串
4.代码
由1.中思路+性质(else),可得代码:
void nxt()
{
n[0] = -1;
int k = -1;
int j = 0;
while(j < t.length())
{
if(k == -1 || t[j] == t[k])
{
j++,k++;
n[j] = k;
}
else k = n[k];
}
}
第二种写法:
for(int i = 2,j = 0;i <= strlen(s + 1);i++)
{
while(j && s[i] != s[j + 1]) j = nxt[j];
if(s[i] == s[j + 1]) j++;
nxt[i] = j;
}