字符串查找算法之(一)KMP算法
2011-06-21 18:51
281 查看
问题:查找Text中是否含有Pattern字符串,返回Pattern在Text中的位置。
#include <string.h> #include <iostream> using namespace std; // init the prefix array. when comparing, if text[i] != pattern[j], // then pattern[prefix[j]] should be next check point of pattern against text[i]. // // say y = prefix[j], y stands for the biggest length of the word pattern[0..(y-1)] // that makes (pattern[0..(y-1)] == pattern[(j-y+1)..j]). // In another word, y stand for the next comparation point of pattern. // (y==0) means no such word, the next comparation should shart from pattern[0]. // Note: it's possible that 0<y<(i-y)<j and 0<(i-y)<y<j. // // When comparing, if (text[i] != pattern[j]) then j = prefix[j-1], // then compare again with text[i] until (j==0) // void InitPrefix(const char *pattern, int prefix[]) { int len = strlen(pattern); prefix[0] = 0; for (int i = 1; i < len; i++) { int k = prefix[i - 1]; while ((k > 0) && (pattern[i] != pattern[k])) { k = prefix[k - 1]; } if (pattern[i] == pattern[k]) { prefix[i] = k + 1; } else { prefix[i]=0; } } return; } int StrStr(const char* text, const char* pattern) { if (!text || !pattern || pattern[0] == '/0' || text[0] == '/0') { return -1; } int lenText = strlen(text); int lenPattern = strlen(pattern); if (lenText < lenPattern) { return -1; } int *prefix = new int[lenPattern + 1]; InitPrefix(pattern, prefix); //the prefix array of pattern int index = -1; // the index to be returned. int i = 0; // the compare index in text. int j = 0; // the compare index in pattern while ((text[i] != '/0') && ((lenText - i) >= (lenPattern - j))) { if (pattern[j] == text[i]) { //reach the end of pattern, find match! return. if (pattern[j+1] == '/0') { index = i - lenPattern + 1; break; } // compare next char in pattern and text. j++; i++; } else { if (j == 0) { // pattern[0] != text[i], then skip to text[i+1]. i++; } else { //pattern[j]!= text[i], the next check point should be pattern[prefix[j-1]] j = prefix[j-1]; } } } delete []prefix; prefix=0; return index; }
相关文章推荐
- python算法-字符串查找KMP算法
- 13. 字符串查找(普通算法和kmp算法)
- 字符串查找之模式匹配算法
- 经典的字符串模式匹配算法KMP算法
- 一步一步写算法(之字符串查找 下篇)
- KMP算法--字符串模式匹配算法
- 查找子字符串的算法
- KMP 算法并非字符串查找的优化
- 在字符串中查找子字符串的算法
- KMP算法:查找子字符串
- 字符串模式匹配算法之二:KMP算法
- Java数据结构之字符串模式匹配算法---KMP算法
- KMP算法 Knuth-Morris-Pratt 字符串查找算法
- 在字符串中查找子字符串的算法
- java小算法—查找字符串中第一次没有重复的字符
- 算法一枚:查找字符串中最长的对称子字符串
- 字符串匹配算法(在字符串T中查找是否有与字符串P相同的子串)
- 从面试题中学算法(1)--哈希表查找字符串中第一次仅出现一次的字母
- 字符串匹配的Boyer-Moore算法 作者: 阮一峰 日期: 2013年5月 3日 上一篇文章,我介绍了KMP算法。 但是,它并不是效率最高的算法,实际采用并不多。各种文本编辑器的"查找"功能(Ct
- 【算法题】查找字符串中无重复最长子串的长度