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 }
本文介绍了一种构建字符串匹配Next数组的方法,通过该方法可以高效地处理字符串匹配问题。主要思路是在给定的字符串中找到最长相等前后缀的长度,并将其作为Next数组的元素,以减少不必要的比较。
2834

被折叠的 条评论
为什么被折叠?



