#include<stdio.h>
#include<string.h>
void Next(const char ss[],int next[])
{
int q;
int k=-1;
int m = strlen(ss);
next[0] = -1;
next[1]=0;
for (q = 2; q < m; ++q)
{
while(k > -1 && ss[q] != ss[k+1])
k = next[k];/往前找
if (ss[q] == ss[k+1])
{
k++;
}
next[q] = k;///结算当前位置 NEXT[q]
}
}
int main()
{
int i;
for (i = 0; i < strlen(ss); ++i)
{
printf("%d ",next[i]);
}
printf("\n");
return 0;
}
#include<string.h>
void Next(const char ss[],int next[])
{
int q;
int k=-1;
int m = strlen(ss);
next[0] = -1;
next[1]=0;
for (q = 2; q < m; ++q)
{
while(k > -1 && ss[q] != ss[k+1])
k = next[k];/往前找
if (ss[q] == ss[k+1])
{
k++;
}
next[q] = k;///结算当前位置 NEXT[q]
}
}
int main()
{
int i;
char ss[] = "abababab";
int next[strlen(ss)]={0};
for (i = 0; i < strlen(ss); ++i)
{
printf("%d ",next[i]);
}
printf("\n");
return 0;
}