-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBoyerMoore.h
102 lines (94 loc) · 3 KB
/
BoyerMoore.h
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
#ifndef CH5_BOYERMOORE_H
#define CH5_BOYERMOORE_H
#include <vector>
#include <string>
using std::vector;
using std::string;
/**
* The {@code BoyerMoore} class finds the first occurrence of a pattern string
* in a text string.
* <p>
* This implementation uses the Boyer-Moore algorithm (with the bad-character
* rule, but not the strong good suffix rule).
* <p>
* For additional documentation,
* see <a href="https://algs4.cs.princeton.edu/53substring">Section 5.3</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*/
class BoyerMoore {
public:
/**
* Preprocesses the pattern string.
*
* @param pat the pattern string
*/
BoyerMoore(string pat) : R(256), pat(pat), right(R, -1) {
// position of rightmost occurrence of c in the pattern
for (int j = 0; j < pat.length(); j++)
right[pat[j]] = j;
}
/**
* Preprocesses the pattern string.
*
* @param pattern the pattern string
* @param R the alphabet size
*/
BoyerMoore(vector<char> pattern, int R) : R(R), pattern(pattern), right(R, -1) {
for (int j = 0; j < pattern.size(); j++)
right[pattern[j]] = j;
}
/**
* Returns the index of the first occurrrence of the pattern string
* in the text string.
*
* @param txt the text string
* @return the index of the first occurrence of the pattern string
* in the text string; n if no such match
*/
int search(string txt) {
int m = pat.length();
int n = txt.length();
int skip;
for (int i = 0; i <= n - m; i += skip) {
skip = 0;
for (int j = m - 1; j >= 0; j--) {
if (pat[j] != txt[i + j]) {
skip = std::max(1, j - right[txt[i + j]]);
break;
}
}
if (skip == 0) return i; // found
}
return n; // not found
}
/**
* Returns the index of the first occurrrence of the pattern string
* in the text string.
*
* @param text the text string
* @return the index of the first occurrence of the pattern string
* in the text string; n if no such match
*/
int search(vector<char> &text) {
int m = pattern.size();
int n = text.size();
int skip;
for (int i = 0; i <= n - m; i += skip) {
skip = 0;
for (int j = m - 1; j >= 0; j--) {
if (pattern[j] != text[i + j]) {
skip = std::max(1, j - right[text[i + j]]);
break;
}
}
if (skip == 0) return i; // found
}
return n; // not found
}
private:
int R; // the radix
vector<int> right; // the bad-character skip array
vector<char> pattern; // store the pattern as a character array
string pat; // or as a string
};
#endif //CH5_BOYERMOORE_H