-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathkmp.cpp
73 lines (61 loc) Β· 1.43 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
using namespace std;
// Function to implement the KMP algorithm
void KMP(string text, string pattern)
{
int m = text.length();
int n = pattern.length();
// if pattern is an empty string
if (n == 0)
{
cout << "The pattern occurs with shift 0";
return;
}
// if text's length is less than that of pattern's
if (m < n)
{
cout << "Pattern not found";
return;
}
// next[i] stores the index of the next best partial match
int next[n + 1];
for (int i = 0; i < n + 1; i++) {
next[i] = 0;
}
for (int i = 1; i < n; i++)
{
int j = next[i + 1];
while (j > 0 && pattern[j] != pattern[i]) {
j = next[j];
}
if (j > 0 || pattern[j] == pattern[i]) {
next[i + 1] = j + 1;
}
}
for (int i = 0, j = 0; i < m; i++)
{
if (text[i] == pattern[j])
{
if (++j == n) {
cout << "The pattern occurs with shift " << i - j + 1 << endl;
}
}
else if (j > 0)
{
j = next[j];
i--; // since `i` will be incremented in the next iteration
}
}
}
// Program to implement the KMP algorithm in C++
int main()
{
string text ;
string pattern;
cout<<"Enter the text ";
cin>>text;
cout<<"Enter the pattern ";
cin>>pattern;
KMP(text, pattern);
return 0;
}