forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KMP_ALGO_stringMatching.cpp
69 lines (59 loc) · 1.05 KB
/
KMP_ALGO_stringMatching.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
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
#define N 100000
using namespace std;
/* creating lps array (longest proper prefix)
proper prefix - prefix with whole string not allowed */
int lps[N];
void LPSArray(string pattern){
int i=1,length = 0;
lps[0] = 0; // lps[0] is always 0
int n = pattern.size();
while(i < n){
if(pattern[i] == pattern[length]){
length++;
lps[i] = length;
i++;
}
else{
if(length != 0){
length = lps[length-1];
}
else{
lps[i] = 0;
i++;
}
}
}
}
void KMP(string text, string pattern){
int n = text.size();
int m = pattern.size();
LPSArray(pattern);
int i=0,j=0;
while(i < n){
if(pattern[j] == text[i]){
i++;
j++;
}
// after j successfull matches
if(j == m){
cout<<"Pattern found at index "<<i-j<<endl;
j = lps[j-1];
}
// mismatch happened after j matches
else if( i<n and pattern[j] != text[i]){
if( j != 0)
j = lps[j-1];
else
i++;
}
}
}
int main(){
cout<<"Enter Text: ";
string text,pattern;
cin>>text;
cout<<"Enter Pattern: ";
cin>>pattern;
KMP(text,pattern);
}