普通的暴力字符串匹配,存在太多的无意义的回溯,导致时间复杂度o(mn)
kmp通过next数组实现了字符串的快速匹配
掌握kmp,要理解以下几部分
1.什么是前缀表
2.为什么要用前缀表
3.如何计算前缀表
4.前缀表与next数组的关系
5.利用next数组匹配
6.前缀表不减1实现
什么是前缀表
前缀表是当模式串与文本串不匹配时,要回退的位置
记录下标 i 之前(包括i)有多大长度的相同前缀后缀
前缀: 不包含最后一个字符
后缀 : 不包含第一个字符
为什么要用前缀表
前缀表告诉我们,当前位置不匹配,要跳回的位置
如何计算前缀表,前缀表与next数组的关系
aabaaf
010120
前缀表与next数组的关系
当前缀表不减去1实现时,即nex数组就是前缀表
当前字符j不匹配时,找到next[j-1]的位置,继续匹配
kmp算法时间复杂度 o(n+m)
构造next数组
设置指针 i j
i 指向前缀起始位置
j指向后缀起始位置
初始化 j =0 next[0]
void getnext(int * next,const string &s){ int j = 0; int next[0] = 0; for(int i = 1;i < s.size();i++){ while(j > 0 & s[i] != s[j]){ j = next[j]; } if(s[i] == s[j]) j++; next[i] = j } }
class Solution {
public:
void getNext(int* next, const string& s) {
int j = 0;
next[0] = 0;
for(int i = 1; i < s.size(); i++) {
while (j > 0 && s[i] != s[j]) {
j = next[j - 1];
}
if (s[i] == s[j]) {
j++;
}
next[i] = j;
}
}
int strStr(string haystack, string needle) {
if (needle.size() == 0) {
return 0;
}
int next[needle.size()];
getNext(next, needle);
int j = 0;
for (int i = 0; i < haystack.size(); i++) {
while(j > 0 && haystack[i] != needle[j]) {
j = next[j - 1];
}
if (haystack[i] == needle[j]) {
j++;
}
if (j == needle.size() ) {
return (i - needle.size() + 1);
}
}
return -1;
}
};
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void next(string s, vector<int> & next){
int j = 0;
next[0] = 0;
for(int i = 1; i < s.size(); i++){
while(j > 0 && s[i] != s[j]){
j = next[j - 1];
}
if(s[i] == s[j]){
j++;
}
next[i] = j;
}
for(auto x : next) cout << x << ' ';
}
int kmp(string imp , string s){
if(s.size() == 0) return 0;
int j = 0;
int len = s.size();
vector<int> ne(len);
next(s,ne);
for(int i = 0; i < imp.size(); i++){
while(j > 0 && imp[i] != s[j]){
j = ne[j - 1];
}
if(imp[i] == s[j]){
j++;
}
if(j == len) return i - len + 1;
}
return -1;
}
int main(){
string s1, s2;
cout << "Main string and auxilary string please divide by block" << endl;
cin >> s1 >> s2;
int dest = kmp(s1,s2);
cout <<dest << endl;
return 0;
}