|
| 1 | +// Time: O(n + m + z), n is the total size of patterns |
| 2 | +// , m is the total size of query string |
| 3 | +// , z is the number of all matched strings |
| 4 | +// Space: O(t), t is the total size of ac automata trie |
| 5 | + |
| 6 | + struct AhoNode { |
| 7 | + vector<AhoNode *> children; |
| 8 | + vector<int> indices; |
| 9 | + AhoNode *suffix; |
| 10 | + AhoNode *output; |
| 11 | + AhoNode() : |
| 12 | + children(26, nullptr), |
| 13 | + suffix(nullptr), |
| 14 | + output(nullptr) {} |
| 15 | +}; |
| 16 | + |
| 17 | +class AhoTrie { |
| 18 | +public: |
| 19 | + AhoTrie(const vector<string>& patterns) : root_(createACTrie(patterns)) { |
| 20 | + node_ = createACSuffixAndOutputLinks(root_); |
| 21 | + } |
| 22 | + |
| 23 | + vector<int> step(char letter) { |
| 24 | + while (node_ && !node_->children[letter - 'a']) { |
| 25 | + node_ = node_->suffix; |
| 26 | + } |
| 27 | + node_ = node_ ? node_->children[letter - 'a'] : root_; |
| 28 | + return getACNodeOutputs(node_); |
| 29 | + } |
| 30 | + |
| 31 | +private: |
| 32 | + AhoNode *createACTrie(const vector<string>& patterns) { // Time: O(n), Space: O(t) |
| 33 | + auto root = new AhoNode(); |
| 34 | + for (int i = 0; i < patterns.size(); ++i) { |
| 35 | + auto node = root; |
| 36 | + for (const auto& c : patterns[i]) { |
| 37 | + if (!node->children[c - 'a']) { |
| 38 | + node->children[c - 'a'] = new AhoNode(); |
| 39 | + } |
| 40 | + node = node->children[c - 'a']; |
| 41 | + } |
| 42 | + node->indices.emplace_back(i); |
| 43 | + } |
| 44 | + return root; |
| 45 | + } |
| 46 | + |
| 47 | + AhoNode *createACSuffixAndOutputLinks(AhoNode *root) { // Time: O(n), Space: O(t) |
| 48 | + queue<AhoNode *> q; |
| 49 | + for (auto node : root->children) { |
| 50 | + if (!node) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + q.emplace(node); |
| 54 | + node->suffix = root; |
| 55 | + } |
| 56 | + |
| 57 | + while (!q.empty()) { |
| 58 | + auto node = q.front(); q.pop(); |
| 59 | + for (int c = 0; c < node->children.size(); ++c) { |
| 60 | + if (!node->children[c]) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + auto child = node->children[c]; |
| 64 | + q.emplace(child); |
| 65 | + auto suffix = node->suffix; |
| 66 | + while (suffix && !suffix->children[c]) { |
| 67 | + suffix = suffix->suffix; |
| 68 | + } |
| 69 | + child->suffix = suffix ? suffix->children[c] : root; |
| 70 | + child->output = !child->suffix->indices.empty() ? |
| 71 | + child->suffix : child->suffix->output; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return root; |
| 76 | + } |
| 77 | + |
| 78 | + vector<int> getACNodeOutputs(AhoNode *node) { // Time: O(z) |
| 79 | + vector<int> result; |
| 80 | + for (const auto& i : node_->indices) { |
| 81 | + result.emplace_back(i); |
| 82 | + // return result; |
| 83 | + } |
| 84 | + auto output = node_->output; |
| 85 | + while (output) { |
| 86 | + for (const auto& i : output->indices) { |
| 87 | + result.emplace_back(i); |
| 88 | + // return result; |
| 89 | + } |
| 90 | + output = output->output; |
| 91 | + } |
| 92 | + return result; |
| 93 | + } |
| 94 | + |
| 95 | + AhoNode * const root_; |
| 96 | + AhoNode *node_; |
| 97 | +}; |
| 98 | + |
| 99 | +class Solution { |
| 100 | +public: |
| 101 | + vector<vector<int>> indexPairs(string text, vector<string>& words) { |
| 102 | + for (auto& word : words) { |
| 103 | + reverse(word.begin(), word.end()); |
| 104 | + } |
| 105 | + AhoTrie trie(words); |
| 106 | + vector<vector<int>> result; |
| 107 | + for (int i = text.length() - 1; i >= 0; --i) { |
| 108 | + for (const auto& j : trie.step(text[i])) { |
| 109 | + result.push_back({i, i + words[j].length() - 1}); |
| 110 | + } |
| 111 | + } |
| 112 | + reverse(result.begin(), result.end()); |
| 113 | + return result; |
| 114 | + } |
| 115 | +}; |
0 commit comments