Next [0] =-1, assuming next [J] = K, that is, P [0... k-1] = P [J-K, J-1]
1) if p [J] = P [K], next [J + 1] = k + 1;
2) If P [J]! = P [K], then K = next [K].
#include <stdio.h>#include <string.h>#include <stdlib.h>void get_next(char *pat, int next[]){ int i, j, plen; plen = strlen(pat); i = 0; j = -1; next[0] = -1; while(i < plen-1){ if(j == -1 || pat[i] == pat[j]){ ++i; ++j; next[i] = j; } else{ j = next[j]; } }}int kmp_match(char *str, char *pat){ int slen, plen, i, j; int *next; if(str == NULL || pat == NULL){ return -1; } slen = strlen(str); if(slen <= 0){ return -1; } plen = strlen(pat); if(plen <= 0){ return -1; } if(slen < plen){ return 0; } next = (int *)malloc(plen*sizeof(int)); if(next == NULL){ return -1; } get_next(pat, next); i = 0; j = 0; while(i < slen){ if(j == -1 || str[i] == pat[j]){ i++; j++; } else{ j = next[j]; } if(j == plen){ free(next); return (i-plen); } } free(next); return 0;}int main(int argc, char *argv[]){ int ret; if(argc != 3){ printf("usage: a.out [patter] [string] \n"); return -1; } ret = kmp_match(argv[2], argv[1]); printf("ret = %d\n", ret); return 0;}