C#实现KMP算法
看了两天的KMP算法,总有有点懂了,这篇文章的帮助很大。依样画瓢写了个C#的KMP算法实现。
以下是代码:
/// <summary> /// 串的模式匹配 KMP算法 /// </summary> /// <param name="str"></param> /// <param name="model"></param> /// <param name="pos"></param> /// <returns></returns> private static int KmpMatch(char[] str, char[] model,int pos) { int loc = -1; if (pos < 1 || pos > str.Length) { return loc; } int i = pos - 1; int j = 0; int[] next = GetNext(model); while (i < str.Length && j < model.Length) { if (j == -1 || str[i] == model[j]) { i++; j++; } else j = next[j]; } if (j >= model.Length) loc = i - model.Length; return loc; } /// <summary> /// 求next[] /// </summary> /// <param name="T"></param> /// <returns></returns> private static int[] GetNext(char[] T) { int[] next = new int[T.Length]; next[0] = -1; int i = 0, j = -1; while (i < T.Length - 1) { if (j == -1 || T[i] == T[j]) { ++i; ++j; if (T[i] != T[j]) next[i] = j; else next[i] = next[j]; } else j = next[j]; } return next; }