Skip to content
Snippets Groups Projects
Commit 890f953c authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Add KMP

parent 5b61c8b8
No related branches found
No related tags found
No related merge requests found
/**
* Knuth-Morris-Pratt - KMP
*
* Complexity (Time):
* preprocess -> O(m)
* search -> O(n)
* Complexity (Space): O(n + m)
*/
int table[MAX];
vector<int> occurs;
// Build the table where table[i] is the longest prefix of patt[0..i] which is
// also a sufix of patt[0..i]
void preprocess(string patt) {
int i = 1, len = 0;
while (i < patt.size()) {
if (patt[i] == patt[len])
table[i++] = ++len;
else if (len)
len = table[len - 1];
else
table[i++] = 0;
}
}
// Search for occurrences of patt in txt and add indexes of matches to occurs
void search(string patt, string txt) {
int i = 0, j = 0;
while (i < txt.size()) {
if (patt[j] == txt[i])
i++, j++;
if (j == patt.size()) {
occurs.push_back(i - j); // Pattern found at (i - j)
j = table[j - 1];
} else if (i < txt.size() && patt[j] != txt[i]) {
if (j) j = table[j - 1];
else i++;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment