|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <map> |
| 4 | +#include <string> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +// Palindromic tree / Eertree (回文木) |
| 8 | +namespace palindromic_tree { |
| 9 | + |
| 10 | +template <class Key> class Node { |
| 11 | + int suffix_link_; // このノードからのsuffix link (suffix の最長回文) |
| 12 | + int length_; // このノードが表す回文の長さ。 -1 となる場合もあるので注意 |
| 13 | + std::map<Key, int> children; |
| 14 | + |
| 15 | +public: |
| 16 | + explicit Node(int suffix_link, int length) : suffix_link_(suffix_link), length_(length) {} |
| 17 | + |
| 18 | + int suffix_link() const { return suffix_link_; } |
| 19 | + |
| 20 | + int length() const { return length_; } |
| 21 | + |
| 22 | + int get_child(Key c) const { |
| 23 | + auto it = children.find(c); |
| 24 | + return (it == children.end()) ? -1 : it->second; |
| 25 | + } |
| 26 | + |
| 27 | + void set_child(int c, int nxt_idx) { children[c] = nxt_idx; } |
| 28 | + |
| 29 | + template <class OStream> friend OStream &operator<<(OStream &os, const Node &node) { |
| 30 | + os << "Node(suffix_link=" << node.suffix_link() << ", length=" << node.length() |
| 31 | + << ", children={"; |
| 32 | + for (const auto &[c, nxt] : node.children) os << c << "->" << nxt << ", "; |
| 33 | + return os << "})"; |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +// Palindromic tree |
| 38 | +// nodes[0] は長さ -1, nodes[1] は長さ 1 のダミーノード |
| 39 | +template <class Key> struct Tree { |
| 40 | + std::vector<Node<Key>> nodes; |
| 41 | + |
| 42 | + Tree() { nodes = {Node<Key>(-1, -1), Node<Key>(0, 0)}; } |
| 43 | + |
| 44 | + // nodes[cursor] は s[0:i] の suffix palindrome を表す |
| 45 | + // 本関数はその nodes[cursor] の suffix palindrome であって更に s[0:(i + 1)] の suffix link となりうる最長のものを返す |
| 46 | + int find_next_suffix(const std::vector<Key> &s, int i, int cursor) { |
| 47 | + while (true) { |
| 48 | + if (cursor < 0) return 0; |
| 49 | + |
| 50 | + const int cur_len = nodes.at(cursor).length(); |
| 51 | + const int opposite_pos = i - cur_len - 1; |
| 52 | + if (opposite_pos >= 0 and s.at(opposite_pos) == s.at(i)) return cursor; |
| 53 | + cursor = nodes.at(cursor).suffix_link(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // 文字列 s を追加する。 Complexity: O(|s|) |
| 58 | + // callback(i, cursor) は s[0:(i + 1)] が追加された後の nodes[cursor] に対して行う処理 |
| 59 | + template <class Callback> void add_string(const std::vector<Key> &s, Callback callback) { |
| 60 | + int cursor = 1; |
| 61 | + |
| 62 | + for (int i = 0; i < (int)s.size(); ++i) { |
| 63 | + |
| 64 | + cursor = find_next_suffix(s, i, cursor); |
| 65 | + |
| 66 | + int ch = nodes.at(cursor).get_child(s.at(i)); |
| 67 | + |
| 68 | + if (ch < 0) { |
| 69 | + const int nxt_cursor = nodes.size(); |
| 70 | + const int new_length = nodes.at(cursor).length() + 2; |
| 71 | + |
| 72 | + int new_suffix_link_par = find_next_suffix(s, i, nodes.at(cursor).suffix_link()); |
| 73 | + int new_suffix_link = nodes.at(new_suffix_link_par).get_child(s.at(i)); |
| 74 | + if (new_suffix_link < 0) new_suffix_link = 1; |
| 75 | + |
| 76 | + nodes.at(cursor).set_child(s.at(i), nxt_cursor); |
| 77 | + nodes.push_back(Node<Key>(new_suffix_link, new_length)); |
| 78 | + cursor = nxt_cursor; |
| 79 | + |
| 80 | + } else { |
| 81 | + cursor = ch; |
| 82 | + } |
| 83 | + |
| 84 | + callback(i, cursor); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + template <class Callback> void add_string(const std::string &s, Callback callback) { |
| 89 | + add_string(std::vector<Key>{s.cbegin(), s.cend()}, callback); |
| 90 | + } |
| 91 | + |
| 92 | + template <class Vec> void add_string(const Vec &s) { |
| 93 | + add_string(s, [](int, int) {}); |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +} // namespace palindromic_tree |
0 commit comments