KMP算法 字符串匹配
来源:互联网 发布:最好用的网络机顶盒 编辑:程序博客网 时间:2024/06/07 01:57
按照上文的思路,自己写了一个KMP算法来实现字符串匹配,可以判断短字符串是否匹配长字符串以及第一次匹配成功时长字符串的初始位置。
class KMPClass { string longString; bool IsMatch; int matchPlace;//匹配数组在长数组中第一个字符的位置 public string LongString { get { return longString; } set { longString = value; } } string shortString; public string ShortString { get { return shortString; } set { shortString = value; } } int[] next; public KMPClass() { next = new int[300]; } public void GetNext() { int len = 0; while (len <= shortString.Length) { int maxLen = 0; for (int i = 1; i <= len / 2; i++) { bool flag = true; for (int j = 0; j < i; j++) { if (shortString[j] != shortString[len - i + j]) { flag = false; break; } } if (flag == true) { maxLen = i; } } next[len] = maxLen; len++; } } public void IsMatchOrNot() { if (longString=="") { IsMatch = false; return; } GetNext(); int shortPosition=0; int longPosition=0; int shortLen = shortString.Length; int longLen = longString.Length; while (true) { if (shortPosition==shortLen) { IsMatch = true; matchPlace = longPosition - shortLen ; return; } if (longPosition==longLen) { IsMatch = false; return; } if (shortString[shortPosition]==longString[longPosition]) { shortPosition++; longPosition++; } else { shortPosition=next[shortPosition+1]; longPosition = longPosition +shortPosition+1- next[shortPosition + 1]; } } } }
- KMP 字符串匹配算法
- kmp字符串匹配算法
- kmp字符串匹配算法
- KMP字符串匹配算法
- 字符串匹配算法-kmp
- KMP(字符串匹配)算法
- KMP 字符串匹配算法
- 字符串匹配算法:KMP
- KMP算法 字符串匹配
- 字符串匹配 KMP 算法
- KMP字符串匹配算法
- KMP字符串匹配算法
- KMP算法:字符串匹配
- KMP字符串匹配算法
- 字符串匹配--KMP算法
- KMP字符串匹配算法
- KMP字符串匹配算法
- KMP字符串匹配算法
- 设备文件和IO
- python初步入门
- linux内核中的位运算
- java学习之路--单例模式的实现
- 最效率的素数序列数组
- KMP算法 字符串匹配
- 字典树
- 第1次实验
- DP堆积木的最大高度
- C++ string中find() ,rfind() 等函数 用法总结及示例
- C++指针一个鲜见知识点
- rqnoj-356-mty的格斗-二维背包
- SWT table 设置行,列背景色,字体色
- Java一道笔试题 equals