|
| 1 | +#ifndef CH5_BOYERMOORE_H |
| 2 | +#define CH5_BOYERMOORE_H |
| 3 | + |
| 4 | +#include <vector> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +using std::vector; |
| 8 | +using std::string; |
| 9 | + |
| 10 | +/** |
| 11 | + * The {@code BoyerMoore} class finds the first occurrence of a pattern string |
| 12 | + * in a text string. |
| 13 | + * <p> |
| 14 | + * This implementation uses the Boyer-Moore algorithm (with the bad-character |
| 15 | + * rule, but not the strong good suffix rule). |
| 16 | + * <p> |
| 17 | + * For additional documentation, |
| 18 | + * see <a href="https://algs4.cs.princeton.edu/53substring">Section 5.3</a> of |
| 19 | + * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne. |
| 20 | + */ |
| 21 | +class BoyerMoore { |
| 22 | +public: |
| 23 | + /** |
| 24 | + * Preprocesses the pattern string. |
| 25 | + * |
| 26 | + * @param pat the pattern string |
| 27 | + */ |
| 28 | + BoyerMoore(string pat) : R(256), pat(pat), right(R, -1) { |
| 29 | + // position of rightmost occurrence of c in the pattern |
| 30 | + for (int j = 0; j < pat.length(); j++) |
| 31 | + right[pat[j]] = j; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Preprocesses the pattern string. |
| 36 | + * |
| 37 | + * @param pattern the pattern string |
| 38 | + * @param R the alphabet size |
| 39 | + */ |
| 40 | + BoyerMoore(vector<char> pattern, int R) : R(R), pattern(pattern), right(R, -1) { |
| 41 | + for (int j = 0; j < pattern.size(); j++) |
| 42 | + right[pattern[j]] = j; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns the index of the first occurrrence of the pattern string |
| 47 | + * in the text string. |
| 48 | + * |
| 49 | + * @param txt the text string |
| 50 | + * @return the index of the first occurrence of the pattern string |
| 51 | + * in the text string; n if no such match |
| 52 | + */ |
| 53 | + int search(string txt) { |
| 54 | + int m = pat.length(); |
| 55 | + int n = txt.length(); |
| 56 | + int skip; |
| 57 | + for (int i = 0; i <= n - m; i += skip) { |
| 58 | + skip = 0; |
| 59 | + for (int j = m - 1; j >= 0; j--) { |
| 60 | + if (pat[j] != txt[i + j]) { |
| 61 | + skip = std::max(1, j - right[txt[i + j]]); |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + if (skip == 0) return i; // found |
| 66 | + } |
| 67 | + return n; // not found |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns the index of the first occurrrence of the pattern string |
| 72 | + * in the text string. |
| 73 | + * |
| 74 | + * @param text the text string |
| 75 | + * @return the index of the first occurrence of the pattern string |
| 76 | + * in the text string; n if no such match |
| 77 | + */ |
| 78 | + int search(vector<char> &text) { |
| 79 | + int m = pattern.size(); |
| 80 | + int n = text.size(); |
| 81 | + int skip; |
| 82 | + for (int i = 0; i <= n - m; i += skip) { |
| 83 | + skip = 0; |
| 84 | + for (int j = m - 1; j >= 0; j--) { |
| 85 | + if (pattern[j] != text[i + j]) { |
| 86 | + skip = std::max(1, j - right[text[i + j]]); |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + if (skip == 0) return i; // found |
| 91 | + } |
| 92 | + return n; // not found |
| 93 | + } |
| 94 | + |
| 95 | +private: |
| 96 | + int R; // the radix |
| 97 | + vector<int> right; // the bad-character skip array |
| 98 | + vector<char> pattern; // store the pattern as a character array |
| 99 | + string pat; // or as a string |
| 100 | +}; |
| 101 | + |
| 102 | +#endif //CH5_BOYERMOORE_H |
0 commit comments