-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKMP.cpp
More file actions
44 lines (38 loc) · 1.08 KB
/
KMP.cpp
File metadata and controls
44 lines (38 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;
vector<int> Pi(string &t){
vector<int> p(t.size(), 0);
for(int i=1, j=0; i<t.size(); i++){
while(j > 0 && t[j] != t[i]) j = p[j-1];
if(t[j] == t[i]) j++;
p[i] = j;
}
return p;
}
vector<int> kmp(string &s, string &t){
vector<int> p = Pi(t), occ;
for(int i=0, j=0; i<s.size(); i++){
while( j > 0 && s[i] != t[j]) j = p[j-1];
if(s[i]==t[j]) j++;
if(j == t.size()) occ.push_back(i-j+1), j = p[j-1];
}
return occ;
}
/*BLOCK_DESC_BEGIN **Optional:** KMP Automato. j = state atual [root=j=0] BLOCK_DESC_END*/
struct Automato {
vector<int> p;
string t;
Automato(string &t) : t(t), p(Pi(t)){}
int next(int j, char c){ //return nxt state
if(final(j)) j = p[j-1];
while(j && c != t[j]) j = p[j-1];
return j + (c == t[j]);
}
bool final(int j){ return j == t.size(); }
};
/**************************
**KMP** - Knuth–Morris–Pratt Pattern Searching
Complexity: O(|S|+|T|)
kmp(s, t) -> returns all occurences of t in s
p = Pi(t) -> p[i] = biggest prefix that is a sufix of t[0,i]
***************************/