70,021
社区成员




int *getNext(char *p, int *next)
{
int k=-1, j=0;
next[0] = -1;
while (j<strlen(p))
{
//if (k=-1 || p[k]==p[j])
if (k==-1 || p[k]==p[j])
{
next[++j] = ++k;
}
else
{
k = next[k];
}
}
return next;
}
int strStr(char * haystack, char * needle){
if (needle==NULL) return 0;
int x = strlen(needle);
int i=0, j=0;
int *next = (int *)malloc(x*sizeof(int));
getNext(needle, next);
while (i<strlen(haystack) && j<strlen(needle))
{
//if (j=-1 || haystack[i]==needle[j])
if (j==-1 || haystack[i]==needle[j])
{
i++;
j++;
}
else
{
j = next[j];
}
}
if (j==strlen(needle))
return i-j;
else
return -1;
}