int *next = NULL;
void get_next(const char * p)
{
assert(p != NULL);
int m =strlen(p);
next= (int*)malloc(m *sizeof(int));
if(next ==NULL)
{
throw new std::exception(" malloc failed!");
}
int k =0;
next[0] = 0;
next[1] = 0;
for(int i = 2; i <= m; ++ i)
{
while(k >0 &&*(p + k +1) != *(p + i))
{
k= next[k];
}
if(*(p+ k + 1) == *(p + i))
{
k++;
}
next[i]= k;
}
}
int kmp_match(const char * T, const char * P)
{
assert(T != NULL &&P != NULL);
int n =strlen(T);
int m =strlen(P);
int k =0;
for(int i = 1; i <= n; i++)
{
while(k >0 &&T[i] != P[k +1])
{
k= next[k];
}
if(T[i] ==p[k + 1])
{
k++;
}
if(k ==m)
{
printf("find it at %d\n", i);
k= 0;
}
}
free next;
return 0;
}