|
| 1 | +#ifndef CH5_KMP_H |
| 2 | +#define CH5_KMP_H |
| 3 | + |
| 4 | +#include <vector> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +using std::string; |
| 8 | +using std::vector; |
| 9 | + |
| 10 | +/** |
| 11 | + * The {@code KMP} class finds the first occurrence of a pattern string |
| 12 | + * in a text string. |
| 13 | + * <p> |
| 14 | + * This implementation uses a version of the Knuth-Morris-Pratt substring search |
| 15 | + * algorithm. The version takes time proportional to <em>n</em> + <em>m R</em> |
| 16 | + * in the worst case, where <em>n</em> is the length of the text string, |
| 17 | + * <em>m</em> is the length of the pattern, and <em>R</em> is the alphabet size. |
| 18 | + * It uses extra space proportional to <em>m R</em>. |
| 19 | + * <p> |
| 20 | + * For additional documentation, |
| 21 | + * see <a href="https://algs4.cs.princeton.edu/53substring">Section 5.3</a> of |
| 22 | + * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne. |
| 23 | + */ |
| 24 | +class KMP { |
| 25 | +public: |
| 26 | + /** |
| 27 | + * Preprocesses the pattern string. |
| 28 | + * |
| 29 | + * @param pat the pattern string |
| 30 | + */ |
| 31 | + KMP(string pat) : R(256), pat(pat), dfa(R, vector<int>(pat.length(), 0)) { |
| 32 | + // build DFA from pattern |
| 33 | + int m = pat.length(); |
| 34 | + dfa[pat[0]][0] = 1; |
| 35 | + for (int x = 0, j = 1; j < m; j++) { |
| 36 | + for (int c = 0; c < R; c++) |
| 37 | + dfa[c][j] = dfa[c][x]; // Copy mismatch cases. |
| 38 | + dfa[pat[j]][j] = j + 1; // Set match case. |
| 39 | + x = dfa[pat[j]][x]; // Update restart state. |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Preprocesses the pattern string. |
| 45 | + * |
| 46 | + * @param pattern the pattern string |
| 47 | + * @param R the alphabet size |
| 48 | + */ |
| 49 | + KMP(vector<char> &pattern, int R) : R(R), pattern(pattern), dfa(R, vector<int>(pattern.size(), 0)) { |
| 50 | + int m = pattern.size(); |
| 51 | + dfa[pattern[0]][0] = 1; |
| 52 | + for (int x = 0, j = 1; j < m; j++) { |
| 53 | + for (int c = 0; c < R; c++) |
| 54 | + dfa[c][j] = dfa[c][x]; // Copy mismatch cases. |
| 55 | + dfa[pattern[j]][j] = j + 1; // Set match case. |
| 56 | + x = dfa[pattern[j]][x]; // Update restart state. |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns the index of the first occurrrence of the pattern string |
| 62 | + * in the text string. |
| 63 | + * |
| 64 | + * @param txt the text string |
| 65 | + * @return the index of the first occurrence of the pattern string |
| 66 | + * in the text string; N if no such match |
| 67 | + */ |
| 68 | + int search(string &txt) { |
| 69 | + // simulate operation of DFA on text |
| 70 | + int m = pat.length(); |
| 71 | + int n = txt.length(); |
| 72 | + int i, j; |
| 73 | + for (i = 0, j = 0; i < n && j < m; i++) { |
| 74 | + j = dfa[txt[i]][j]; |
| 75 | + } |
| 76 | + if (j == m) return i - m; // found |
| 77 | + return n; // not found |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the index of the first occurrrence of the pattern string |
| 82 | + * in the text string. |
| 83 | + * |
| 84 | + * @param text the text string |
| 85 | + * @return the index of the first occurrence of the pattern string |
| 86 | + * in the text string; N if no such match |
| 87 | + */ |
| 88 | + int search(vector<char> &text) { |
| 89 | + |
| 90 | + // simulate operation of DFA on text |
| 91 | + int m = pattern.size(); |
| 92 | + int n = text.size(); |
| 93 | + int i, j; |
| 94 | + for (i = 0, j = 0; i < n && j < m; i++) { |
| 95 | + j = dfa[text[i]][j]; |
| 96 | + } |
| 97 | + if (j == m) return i - m; // found |
| 98 | + return n; // not found |
| 99 | + } |
| 100 | + |
| 101 | +private: |
| 102 | + const int R; // the radix |
| 103 | + vector<vector<int>> dfa; // the KMP automoton |
| 104 | + vector<char> pattern; // either the character array for the pattern |
| 105 | + string pat; // or the pattern string |
| 106 | +}; |
| 107 | + |
| 108 | + |
| 109 | +#endif //CH5_KMP_H |
0 commit comments