Knuth–Morris–Pratt Algorithm
Knuth–Morris–Pratt (a.k.a KMP Algorithm) is a string search algorithm. it searches for occurrences of a sub-string within a main-string by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to determine where the next match could begin, thus bypassing re-examination of previously matched characters.
/*****Please include following header files*****/
// string
// vector
/***********************************************/
/*****Please use following namespaces*****/
// std
/*****************************************/
static void ComputeLPSArray(string pat, int m, int* lps) {
int len = 0;
int i = 1;
lps[0] = 0;
while (i < m)
{
if (pat[i] == pat[len])
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0)
{
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
}
static vector<int> SearchString(string str, string pat) {
vector<int> retVal;
int M = pat.length();
int N = str.length();
int i = 0;
int j = 0;
int* lps = new int[M];
ComputeLPSArray(pat, M, lps);
while (i < N)
{
if (pat[j] == str[i])
{
j++;
i++;
}
if (j == M)
{
retVal.push_back(i - j);
j = lps[j - 1];
}
else if (i < N && pat[j] != str[i])
{
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
delete[] lps;
return retVal;
}
Example
string data = "the quick brown fox jumps over the lazy dog";
vector<int> value = SearchString(data, "the");
Output
0
31