forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Knuth_Morris_Pratt.cpp
108 lines (88 loc) · 1.96 KB
/
Knuth_Morris_Pratt.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
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
// https://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm
// Time Complexity:-O(N+M).
// Space Complexity:-O(M).
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Fills lps for given patttern pat[0..M-1]
void computeLPS(string &patt, vector<int> &lps)
{
int M = patt.size();
// length of the previous longest prefix suffix
int len = 0;
lps[0] = 0;
int i = 1;
// the loop calculates lps[i] for i = 1 to M-1
while (i < M)
{
if (patt[i] == patt[len])
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0)
{
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
}
// Prints occurrences of patt in text
void searchByKMP(string &text, string &patt)
{
int N = text.size(), M = patt.size();
// create lps vector that will hold the longest prefix suffix values for pattern
vector<int> lps(M);
// Preprocess the pattern(calculate lps vector)
computeLPS(patt, lps);
int i = 0, j = 0;
while (i < N)
{
if (patt[j] == text[i])
{
i++;
j++;
}
if (j == M)
{
cout << "Pattern found at index " << i - j << endl;
j = lps[j - 1];
}
else if (i < N && patt[j] != text[i])
{
if (j == 0)
i++;
else
j = lps[j - 1];
}
}
}
int main()
{
string text = "", patt = "";
int n;
cout << "Enter Number of test Cases:";
cin >> n;
while (n)
{
cout << endl;
cout << "Enter the text string:";
cin >> text;
cout << endl << "Enter the pattern string:";
cin >> patt;
searchByKMP(text, patt);
n--;
text.clear();
patt.clear();
}
return 0;
}