-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmp.cpp
More file actions
88 lines (88 loc) · 1.83 KB
/
kmp.cpp
File metadata and controls
88 lines (88 loc) · 1.83 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
#include<iostream>
#include<cstring>
#include<stdlib.h>
using namespace std;
class KMP
{
char *pattern;
int *lps;
int M;
public:
KMP(){}
KMP(char *pat);
void printlps();
void Compute_Prefix();
void KMPMatcher(char *text);
};
KMP::KMP(char *pat)
{
M = strlen(pat);
pattern = new char[M+2];
for (int i=1; i<=M+1; i++)
pattern[i]= *(pat+i-1);
lps = new int[M+1];
Compute_Prefix();
}
void KMP::Compute_Prefix()
{
int k = 0;
lps[1] = 0;
for (int q=2; q<=M; q++)
{
while (k>0 && pattern[k+1] != pattern[q])
k = lps[k];
if (pattern[k+1] == pattern[q])
k++;
lps[q] = k;
}
}
void KMP::printlps()
{
for (int i=1; i<=M; i++)
cout << lps[i] << ", ";
}
void KMP::KMPMatcher(char *text)
{
int q = 0;
int N=strlen(text);
for (int i=0; i<N; i++)
{
cout << i <<": " << q;
while (q>0 && pattern[q+1] != text[i])
{
q = lps[q];
cout << ", " << q;
}
if (pattern[q+1] == text[i])
q++;
if (q == M)
{
cout << "Pattern found at position " << i-M+1 << endl;
q = lps[q];
}
cout << endl;
}
cout << q << endl;
}
int main()
{
char txt[50]; //"ABABDABABCABABCABAB";
char pat[10]; //"ABABCABAB";
char ch='y';
cout << "Enter the pattern : ";
cin >> pat;
KMP K(pat);
cout << "The preprocessed longest suffix array for the given pattern : "<<endl;
K.printlps();
cout << endl;
do
{
cout << "Enter the text : ";
cin >> txt;
K.KMPMatcher(txt);
cout << "Do you want to check for the same pattern in another text?(y/n) ";
cin >> ch;
}
while (ch == 'y' || ch == 'Y');
return 0;
}