-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoth Algorithms
More file actions
189 lines (147 loc) · 4.16 KB
/
Both Algorithms
File metadata and controls
189 lines (147 loc) · 4.16 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
// includes for testing runtimes
#include <chrono>
#include <ctime>
#include <time.h>
#include <string.h>
void badHeuristic(string str, int size, std::vector<int> * badchar) {
// Initialize everything at -1
for (int i = 0; i < 256; i++) {
badchar->push_back(-1);
}
// fill the value of last occurrence of the last character
for (int j = 0; j < size; j++) {
badchar->at((int)str[j]) = j;
}
}
// gives an LPS array from a character list
void LPS(char* pat, int M, int* LPSList){
// len of the previous longest prefix suffix
int len = 0;
LPSList[0] = 0; // LPSList[0] will always be 0
// calculates LPSList[x]
int x = 1;
while (x < M) {
if (pat[x] == pat[len]) {
len++;
LPSList[x] = len;
x++;
} else {
if (len != 0) {
len = LPSList[len - 1];
} else {
LPSList[x] = 0;
x++;
}
}
}
}
std::vector<int> boyerMoore(string pattern, string text) {
int patSize = pattern.size();
int textSize = text.size();
// vector for the solutions
std::vector<int> solutions;
std::vector<int> badchar;
// Fill the bad character array
badHeuristic(pattern, patSize, &badchar);
int shift = 0;
while(shift <= (textSize - patSize)) {
//std::cout << "start " << endl;
int i = patSize - 1;
//lower i while characters of pattern and text are the same
//std::cout << "while " << endl;
while(i >= 0 && pattern[i] == text[shift + i]) {
//std::cout << "test 6" << endl;
i = i - 1;
}
//std::cout << "if " << endl;
if (i <= -1) {
solutions.push_back(shift);
// move the pattern so that the next character in text aligns with its last occurrence in pattern.
//std::cout << "if if " << endl;
if (shift + patSize < textSize) {
shift = shift + patSize-badchar[text[shift + patSize]];
//std::cout << "if if in " << endl;
} else {
shift = shift + 1;
//std::cout << "if else in " << endl;
}
} else {
// shift to align with the last occurrence of the incorrect char in pattern
shift = shift + max(1, i - badchar[text[shift + i]]);
//std::cout << "else in " << endl;
}
}
return solutions;
}
std::vector<int> KMP(char* patern, char* txtToCheck) {
int strLenPat = strlen(patern);
int strLenCheck = strlen(txtToCheck);
std::vector<int> solutions;
// array for the LPS values of the pattern
int lps[strLenPat];
// fills the lps array
LPS(patern, strLenPat, lps);
int x = 0; // index for txtToCheck
int y = 0; // index for patern
while ((strLenCheck - x) >= (strLenPat - y)) {
if (patern[y] == txtToCheck[x]) {
y++;
x++;
}
if (y == strLenPat) {
solutions.push_back(x-y);
y = lps[y - 1];
} else if (x < strLenCheck && patern[y] != txtToCheck[x]) {
if (y != 0) {
y = lps[y - 1];
} else {
x++;
}
}
}
return solutions;
}
int main() {
std::cout << "Input the size of the list (ideally larger than 1000 and below 10000000): ";
long long int num;
std::cin >> num;
char text[num];
for (long long int i = 0; i < num - 1; i++) {
text[i] = 'X';
}
text[num - 1] = 'Y';
char pattern[] = "XXY";
std::vector<int> solutions;
//timer
auto start = std::chrono::system_clock::now();
solutions = KMP(pattern, text);
if (solutions.size() != 0) {
for (int i = 0; i < solutions.size(); i++) {
std::cout << "Pattern at idx: " << solutions[i] << std::endl;
}
} else {
std::cout << "The pattern was not found :(" << endl;
}
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
std::cout << "KMP time (seconds): " << std::setprecision(10) << elapsed_seconds.count() << endl;
//std::cout << "KMP time: " << (double)end - (double)start << endl;
// timer
auto start2 = std::chrono::system_clock::now();
solutions = boyerMoore(pattern, text);
if (solutions.size() != 0) {
for (int i = 0; i < solutions.size(); i++) {
std::cout << "Pattern at idx: " << solutions[i] << std::endl;
}
} else {
std::cout << "The pattern was not found :(" << endl;
}
auto end2 = std::chrono::system_clock::now();
elapsed_seconds = end2-start2;
std::cout << "boyerMoore time: " << std::setprecision(10) << elapsed_seconds.count() << endl;
return 0;
}