-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBM.cpp
More file actions
38 lines (33 loc) · 1.39 KB
/
BM.cpp
File metadata and controls
38 lines (33 loc) · 1.39 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
#include <iostream>
#include <string>
#include <unordered_map>
#include <iomanip>
#include "BM.h"
using namespace std;
void BMSearch(const string& txt, const string& pattern) { //Boyer-Moore Algorithm, using bad character heuristic
int count = 0; // # occurrences in the text
int patSize = pattern.length();
int txtSize = txt.length();
unordered_map<char, int> badChar; // this is used to shift the pattern right if a character is found, according to the int associated with it
for (int i = 0; i < patSize; i++) { //this is the bad match table, where the value will be the length of the pattern - indexofactualcharacter - 1;
badChar[pattern[i]] = max(1, (patSize - i - 1)); //repeated letters in the pattern will be updated in the map
}
//compare the txt index with the rightmost character of the badChar map, if they are the same keep, comparing to the left until the pattern matches the txt
//if the character in the txt is present in the map, but not the rightmost, shift the pattern
int i = patSize - 1;
while (i < txtSize) {
int j = patSize - 1;
while (j >= 0 && txt[i] == pattern[j]) {
i--;
j--;
}
if (j == -1) {
count++;
i += patSize + 1;
}
else {
i += max(badChar[txt[i]], (patSize - j));
}
}
cout << "Occurrences: " << count << endl;
}