C#实现KMP算法
2013-07-18 11:51
274 查看
看了两天的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; }
相关文章推荐
- KMP算法 C#实现 字符串查找简单实现
- 字符串匹配的KMP算法详解及C#实现
- 字符串匹配的KMP算法详解及C#实现
- KMP算法 C#简单实现
- KMP算法的C#实现方法
- 用C#2.0实现网络蜘蛛(WebSpider)
- 用C#实现的邮件发送的例子
- HTTP Basic 验证客户端 C#实现笔记
- C#:注册机的实现【提供源代码下载】
- C#实现读取指定盘符硬盘序列号的方法
- C#实现全局快捷键(系统热键)响应(转)
- C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~
- [转]比较高效地实现从两个不同数组中提取相同部分组成新的数组(只支持Int类型) [C#]
- C#实现二维码扫码登录
- [C#]实现文件复制[更新]实时显示进度条
- C#实现对Word文件读写
- 用C#实现HTTP协议下的多线程文件传输
- C#中实现暂停下载文件和恢复下载文件
- 用C#实现对本机IP地址的设置
- C#接口的实现