-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKMP.cpp
57 lines (48 loc) · 821 Bytes
/
KMP.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
using namespace std;
int main() {
string txt = "AAABBAABBBBAABBAAAAAABBBAAB";
string pat = "BBAA";
int n = txt.size(), m = pat.size();
//creating lps array
int lps[m];
lps[0] = 0;
int k = 1, len = 0;
while (k < m) {
if (pat[len] == pat[k]) {
len++;
lps[k] = len;
k++;
}
else {
if (len > 0) len = lps[len - 1];
else {
lps[k] = 0;
k++;
}
}
}
// for (int i = 1; i < m; i++) {
// int j = i;
// for (; j > -1; j--) {
// if (pat.substr(0, j) == pat.substr(i+1-j, j)) break;
// }
// lps[i] = j;
// }
// Pattern Searching
int i = 0, j = 0;
while (i < n) {
if (j == m) {
cout << i-j << endl;
j = lps[j-1];
}
else if (txt[i] == pat[j]) {
i++; j++;
}
else {
if (j > 0) j = lps[j-1];
else i++;
}
}
}