-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringMatching.cpp
More file actions
103 lines (90 loc) · 2.33 KB
/
StringMatching.cpp
File metadata and controls
103 lines (90 loc) · 2.33 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
/* This code provides the pattern matches using Naïve String-
Matching Algorithm as well as KMP String-Matching Algorithm. */
#include <iostream>
#include <string>
using namespace std;
void NaiveStringMatcher(string, string);
void KMPMatcher(string, string);
int *ComputePrefixFunction(string);
int main()
{
string text, pattern;
cout << endl;
cout << "Enter the text string: " << endl;
cin >> text;
cout << "Enter the pattern string: " << endl;
cin >> pattern;
cout << endl;
cout << "Using Naive String Matching Algorithm:" << endl;
NaiveStringMatcher(text, pattern);
cout << endl;
cout << "Using KMP String Matching Algorithm:" << endl;
KMPMatcher(text, pattern);
cout << endl;
cout << "Devanshu Gupta 21BCE0597\n" << endl;
return 0;
}
// Naive String Matching Function
void NaiveStringMatcher(string text, string pattern)
{
int tLength = text.length();
int pLength = pattern.length();
for (int shift = 0; shift <= tLength - pLength; shift++)
{
int index;
for (index = 0; index < pLength; index++)
{
if (pattern[index] != text[index + shift])
{
break;
}
}
if (index == pLength)
{
cout << "=> Pattern occurs with shift = " << shift << endl;
}
}
}
// KMP String Matching Functions
int *ComputePrefixFunction(string pattern, int pLength)
{
int *piTable = new int[pLength];
piTable[0] = 0;
int k = 0;
for (int q = 1; q < pLength; q++)
{
while ((k > 0) && (pattern[k] != pattern[q]))
{
k = piTable[k - 1];
}
if (pattern[k] == pattern[q])
{
k = k + 1;
}
piTable[q] = k;
}
return piTable;
}
void KMPMatcher(string text, string pattern)
{
int tLength = text.length();
int pLength = pattern.length();
int *piTable = ComputePrefixFunction(pattern, pLength);
int q = 0;
for (int i = 0; i < tLength; i++)
{
while ((q > 0) && (pattern[q] != text[i]))
{
q = piTable[q - 1];
}
if (pattern[q] == text[i])
{
q = q + 1;
}
if (q == pLength)
{
cout << "=> Pattern occurs with shift = " << i - pLength + 1 << endl;
q = piTable[q - 1];
}
}
}