KMP(Knuth-Morris-Pratt)
当T[i] != P[j]时
有T[i-j ~ i-1] == P[0 ~ j-1]
由P[0 ~ k-1] == P[j-k ~ j-1]
必然:T[i-k ~ i-1] == P[0 ~ k-1] -->because k<j
如果给定的模式串是:“ABCDABD”,从左至右遍历整个模式串,其各个子串的前缀后缀分别如下表格所示:

也就是说,原模式串子串对应的各个前缀后缀的公共元素的最大长度表为(下简称《最大长度表》):
GetNext()直接用字符串ABCDABD带入下面算法验证,记住下面算法:
void GetNext(char* p,int next[])
{
int pLen = strlen(p);
next[0] = -1;
int k = -1;
int j = 0;
while (j < pLen - 1)
{
//p[k]表示前缀,p[j]表示后缀
if (k == -1 || p[j] == p[k])
{
++k;
++j;
next[j] = k;
}
else
{
k = next[k];
}
}
}
匹配算法:
int KmpSearch(char* s, char* p)
{
int i = 0;
int j = 0;
int sLen = strlen(s);
int pLen = strlen(p);
while (i < sLen && j < pLen)
{
//①如果j = -1,或者当前字符匹配成功(即S[i] == P[j]),都令i++,j++
if (j == -1 || s[i] == p[j])
{
i++;
j++;
}
else
{
//②如果j != -1,且当前字符匹配失败(即S[i] != P[j]),则令 i 不变,j = next[j]
//next[j]即为j所对应的next值
j = next[j];
}
}
if (j == pLen)
return i - j;
else
return -1;
}
====NEXT数组下标从0开始更容易理解====
Brute Force 暴力算法:
int ViolentMatch(char* s, char* p)
{
int sLen = strlen(s);
int pLen = strlen(p);
int i = 0;
int j = 0;
while (i < sLen && j < pLen)
{
if (s[i] == p[j])
{
//①如果当前字符匹配成功(即S[i] == P[j]),则i++,j++
i++;
j++;
}
else
{
//②如果失配(即S[i]! = P[j]),令i = i - (j - 1),j = 0
i = i - j + 1;
j = 0;
}
}
//匹配成功,返回模式串p在文本串s中的位置,否则返回-1
if (j == pLen)
return i - j;
else
return -1;
}
KMP 算法:
class Solution {
public:
void GenNext(string p, int next[]){
int plen = p.size();
next[0] = 0;
int j = 0;
for(int i=1; i<plen; ){
if(p[j] == p[i]){
next[i] = j+1;
j++;
i++;
}
else{
if(j!=0)
j = next[j-1];
else{
next[i] = 0;
i++;
}
}
}
}
int strStr(string haystack, string needle) {
int slen = haystack.size();
int plen = needle.size();
if(slen < plen) return -1;
if(needle == "" ||haystack == "") return 0;
int i=0, j=0;
int next[plen];
GenNext(needle, next);
while(i<slen && j<plen){
if(haystack[i]==needle[j]){
i++;
j++;
}else{
if(j!=0)
j = next[j-1];
else
i++;
}
}
if(j==plen) return i-j;
return -1;
}
};
BM算法 Sunday算法https://www.cnblogs.com/ZuoAndFutureGirl/p/9028287.html