kmp失效函数(测试博客的编辑器)

 1 int *find_next(char *p)
 2 {
 3     int k = -1;                            //right shift i-k 
 4     int i = 0;
 5     int m = strlen(p);
 6     int *next = (int *)malloc(sizeof (int) * m);        
 7     next[0] = -1;
 8                                         //maintain the k is the length of longest matching prefix of p[i]
 9     while (i < m-1) {                    //after i++, i == m-1, the last index of element in next[]
10         while (k >= 0 && p[i] != p[k])    //how to assure the k is the length of longest matching substring of p[i]??
11             k = next[k];
12         i++;
13         k++;
14         next[i] = k;
15     }
16     return next;
17 }

 

posted @ 2018-06-11 14:44  corsican  阅读(564)  评论(0)    收藏  举报