Repository navigation
Expand file tree
/
Copy pathKMP.cpp
More file actions
124 lines (92 loc) · 3.68 KB
/
Copy pathKMP.cpp
File metadata and controls
124 lines (92 loc) · 3.68 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define M 200005
int lps[M]; // this array denotes the LENGTH of proper(can't include first element) longest suffix of the substring
// which is also a proper prefix(can't include last element).
// e.g
// a a a a a
// 0 1 2 3 4
// a b c d e a b c d
// 0 0 0 0 0 1 2 3 4
// For every mismatch between pattern and text we dont need to start over again. If there is a mismatch between
// text[i] & pattern[j] then we need to fix i and left shift j = lps[j-1]. This is done because
// pattern[0, 'lps[j-1]'] initial substring will anyways match text[i - lps[j-1], i-1] substring.
// same concept is getting used while preprocessing lps array.
// IMPORTANT NOTE:
// let j be the pointer for pattern traversal
// j = lps[j-1] not only allows us to skip the prefix which would have anyways got matched but also makes the pointer (j)
// point to the index one more than the last index of matched prefix.
// i.e
// 0 1 2 3 4 5 6 7 8
// a b c d e a b c d
// 0 0 0 0 0 1 2 3 4
// if mismatch happens at second b (index 6) then lps[6-1] or lps[5] (value lps[5] = 1) not only allows us to skip
// 1 character from the beginning but also makes j(pointer for pattern traversal) point to 1 that is next character
// after the prefix match is over (in this example 'b' with index 1). So from this point onwards comparison starts
// from index (j = 1).
void KMP_search(string text,string pattern){
int n = text.length();
int m = pattern.length();
int i=0;
int j=0;
while(i<n){
if(text[i] == pattern[j]){
i++;
j++;
}
if(j==m){
cout<<"pattern found at position: "<<i-m<<"\n";
j = lps[j-1]; // here j-1 == m-1 i.e last index of the pattern
continue;
}
if(i<n && pattern[j]!=text[i]){
if(j!=0){j = lps[j-1];}
else{i++;}
}
}
return;
}
void compute_lps_array(string pattern){
int n = pattern.length();
lps[0] = 0; // 0 in RHS is the index
int len = 0;
int i=1;
while(i<n){
if(pattern[i] == pattern[len]){
len++;
lps[i] = len;
i++;
}
else{
if(len!=0){len = lps[len-1];}
else{
lps[i] = 0;
i++;
}
}
}
return;
}
void init(){
for(int i=0;i<M;i++){lps[i] = M;}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string text = "abcdhfifabcdnsfdkpoabcaaababc";
string pattern = "abc";
init();
compute_lps_array(pattern);
KMP_search(text,pattern);
return 0;
}
// Time Complexity: outer loop is O(N). Inner loop makes the the pointer 'j' decrease at times. Important observation here is that the decrement can happen only
// as many times as the number of increments (matches) before the mismatch. i.e,
// Text: aaaabaabab
// Pattern: aaaaa
// with 0 based indexing in mind, the first mismatch happens when the inner pointer j=4 (last 'a' of the pattern). Now the decrement will also happen 4 times
// in this case. Next decrement process will happen when i = 7 (second 'b'). In here, since the increment happened twice, therefore the decrement also happened twice.
// So basically to have n negative steps, you need n positive steps & n positive steps means you are done traversing through the whole text.
// So worst time complexity is n + n = 2n which is O(n). The time complexity of building proper prefix suffix table is O(m).
// Overall time complexity O(m+n).