-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathsearchPatterns.cpp
112 lines (83 loc) · 2.4 KB
/
searchPatterns.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
109
110
111
112
//
// Created by Sithum on 2021-01-01.
//
#include "clientClass.h"
#include <iostream>
class patternSearchNaive {
char* pattern;
char* text;
// naive bias pattern search
public: void search(char* pattern, char* text) {
int txt = strlen(text);
int pat = strlen(pattern);
for (int i = 0; i <= txt - pat; i++) {
// for pattern matching
for (int j = 0; j < pat; j++) {
if (text[i + j] != pattern[j]) {
break;
}
// if pattern matches
if (j = pat) {
std::cout << "Pattern found" << i << std::endl;
}
}
}
}
};
//for Knuth morris pratt algorithm
class patternSearchKMP {
//occurance of text[],pattern[]
public:
void search(char* pattern, char* text) {
int txt = strlen(text);
int pat = strlen(pattern);
// to hold longest prefix suffix
// value for pattern
int lps[1];
//preprocess the pattern(calculate lps[] array)
LPS(pattern, pat, lps);
int index = 0;
int j_index = 0;
while (index < txt) {
if (pattern[j_index] == text[index]) {
j_index++; // index for pattern
index++; // index for text
}
if (j_index == pat) {
std::cout << "Index found " << index - j_index << std::endl;
j_index = lps[j_index - 1];
}
//mismatch after pattern match
else if (index < pat && pattern[j_index] != text[index]) {
if (j_index != 0) {
j_index = lps[j_index - 1];
}
else {
index = index++;
}
}
}
}
public: void LPS(char* pattern, int pat, int* lps) {
int lenght = 0;
lps[0] = 0;
// the loop calculates lps[index] for index = 1 to pat-1
int index = 1;
while (index < pat) {
if (pattern[index] == pattern[lenght]) {
lenght++;
lps[index] = lenght;
index++;
}
else {
if (lenght != 0) {
lenght = lps[lenght - 1];
}
else {
lps[index] = 0;
index++;
}
}
}
}
};