shogo-library

競技プログラミング用の C++ ライブラリ

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: Trie
(src/string/trie.hpp)

文字列の集合 (多重集合) を、接頭辞の木で管理する Trie<K, Base> です。文字 c は c - Base として $0$ 以上 $K$ 未満の番号に直します。根はノード 0 で、空文字列に対応します。文字列の操作は、文字列の長さを $L$ として $O(L)$ です。ノードは jump pointer を持つので、2 つのノードの文字列の辞書順の比較や最長共通接頭辞が、ノード数を $n$ として $O(\log n)$ で求められます。

Trie<26, 'a'> trie;                // 英小文字
long long v = trie.insert("abc");  // 追加して、"abc" のノードを返す
trie.insert("abd");
trie.count_prefix("ab");           // 2 ("ab" で始まる文字列の個数)
trie.count("abc");                 // 1
trie.erase("abc");                 // 1 個取り除く
trie.find("x");                    // -1 (ノードがない)

long long a = trie.find("abd"), b = trie.extend(trie.find("ab"), 'a');  // extend は文字列を追加せずにノードを作る
trie.less(b, a);                   // true ("aba" < "abd")
trie.depth(trie.lca(a, b));        // 2 (最長共通接頭辞 "ab" の長さ)

メンバ関数

関数 説明 計算量
Trie<K, Base>() 空の Trie (根だけ)。既定は K = 26、Base = 'a' $O(K)$
insert(s) 文字列 s を 1 個追加し、s のノードを返す。同じ文字列を何度も追加できる $O(LK)$ (新しいノード 1 つに $O(K)$)
erase(s) s を 1 個取り除く。s がなければ何もせず false を返す。ノードは消さない $O(L)$
find(s) s のノード (なければ -1)。s を接頭辞に持つ文字列を一度でも追加していればノードがある $O(L)$
count(s) s の個数 $O(L)$
count_prefix(s) s で始まる文字列の個数 (重複を含む) $O(L)$
longest_common_prefix(s) 追加した文字列のどれかとの最長共通接頭辞の長さの最大値 $O(L)$
total() 文字列の個数 (重複を含む) $O(1)$
size() ノードの個数 (根を含む) $O(1)$
child(v, c) ノード v から文字 c で進んだノード (なければ -1) $O(1)$
extend(v, c) ノード v から文字 c で進んだノード。なければ作る (文字列の個数は変えない) $O(K)$
less(u, v) ノード u の文字列が v の文字列より辞書順で小さいか。一方が他方の接頭辞なら短いほうが小さい $O(\log n)$
lca(u, v) ノード u、v の最小共通祖先 (2 つの文字列の最長共通接頭辞のノード) $O(\log n)$
parent(v) 親 (根なら -1) $O(1)$
depth(v) 深さ (ノードの文字列の長さ) $O(1)$
count_prefix_at(v) ノード v を通る (v の文字列で始まる) 文字列の個数 $O(1)$
count_at(v) ノード v で終わる文字列の個数 $O(1)$

例: すべての組の最長共通接頭辞の長さの和

文字列の組 $(i, j)$ の最長共通接頭辞の長さは、2 つが共通に通る根以外のノードの個数です。そのため、ノードごとに通る文字列の個数 $c$ から $\binom{c}{2}$ を足せば求められます。

long long sum = 0;
for (long long v = 1; v < trie.size(); v++) {
    long long c = trie.count_prefix_at(v);
    sum += c * (c - 1) / 2;
}

例: 辞書順最小の経路

各辺に文字が書かれたグラフで、始点から各頂点への最短路のうち文字列が辞書順最小のものを求めます。BFS の順に、前の層の頂点のノードを extend で伸ばし、less で最小を選びます (ABC481 F)。

auto d = shortest_path_bfs(g, 0).dist;
Trie<9, 1> trie;  // 文字は 1〜9
std::vector<long long> node(n, -1);
node[0] = 0;
for (long long u : argsort(d)) {
    for (auto& e : g[u]) {
        if (d[e.dst] != d[u] + 1) continue;
        long long v = trie.extend(node[u], e.cost);
        if (node[e.dst] == -1 || trie.less(v, node[e.dst])) node[e.dst] = v;
    }
}

関連

Verified with

Code

#pragma once

/**
 * @file trie.hpp
 * @brief Trie
 */

#include <algorithm>
#include <array>
#include <cassert>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>

/**
 * @brief Trie (文字列の集合を接頭辞の木で管理する)
 *
 * 文字 c は c - Base として 0 以上 K 未満の番号に直す。根はノード 0 (空文字列)
 * 同じ文字列を何度も追加でき、多重集合として扱う
 * ノードは jump pointer を持ち、2 つのノードの最小共通祖先や文字列の辞書順の比較が O(log n) でできる
 * 例:
 * Trie<26, 'a'> trie;
 * trie.insert("abc");
 * trie.count_prefix("ab");  // 1
 *
 * @tparam K 文字の種類数
 * @tparam Base 最小の文字
 */
template <int K = 26, int Base = 'a'>
class Trie {
    struct Node {
        std::array<int, K> next;
        int parent, depth;
        int ch;          // 親から来た辺の文字の番号 (根は -1)
        int jump;        // 祖先への飛び先 (深さの差が 1, 1, 3, 1, 1, 3, 7, ... となる skew-binary の飛び方)
        int prefix_cnt;  // このノードを通る (このノードの文字列を接頭辞に持つ) 文字列の個数
        int word_cnt;    // このノードで終わる文字列の個数
    };
    // メモリを抑えるため、ノードの中身は int で持つ。外へ返すときに long long にする
    std::vector<Node> nodes;

    int add_node(int parent, int ch) {
        Node node;
        node.next.fill(-1);
        node.parent = parent;
        node.ch = ch;
        node.prefix_cnt = node.word_cnt = 0;
        if (parent == -1) {
            node.depth = 0;
            node.jump = 0;
        } else {
            node.depth = nodes[parent].depth + 1;
            // 親の飛び先 2 つの深さの差が等しければ、2 つをまとめて飛ぶ
            int j = nodes[parent].jump, jj = nodes[j].jump;
            node.jump = nodes[parent].depth - nodes[j].depth == nodes[j].depth - nodes[jj].depth ? jj : parent;
        }
        nodes.push_back(node);
        return nodes.size() - 1;
    }
    // v の子 i (なければ作る)
    int extend_node(int v, int i) {
        if (nodes[v].next[i] == -1) {
            int u = add_node(v, i);
            nodes[v].next[i] = u;
        }
        return nodes[v].next[i];
    }
    // v の深さ d の祖先
    int ancestor(int v, int d) const {
        while (nodes[v].depth > d) v = nodes[nodes[v].jump].depth >= d ? nodes[v].jump : nodes[v].parent;
        return v;
    }
    // 深さが等しく異なる u, v を、親が一致する直前まで持ち上げる (飛び先は深さだけで決まるので、そろって飛べる)
    std::pair<int, int> below_lca(int u, int v) const {
        while (nodes[u].parent != nodes[v].parent) {
            if (nodes[u].jump != nodes[v].jump) {
                u = nodes[u].jump, v = nodes[v].jump;
            } else {
                u = nodes[u].parent, v = nodes[v].parent;
            }
        }
        return {u, v};
    }
    // 文字列リテラル ("abc" は char[4]) を末尾の '\0' まで読まないよう、文字列は string_view にする
    template <typename R>
    static decltype(auto) as_range(const R& s) {
        if constexpr (std::is_convertible_v<const R&, std::string_view>) {
            return std::string_view(s);
        } else {
            return (s);
        }
    }
    static int index(int c) {
        assert(0 <= c - Base && c - Base < K);
        return c - Base;
    }
    // 文字列 s に対応するノード (なければ -1)
    template <typename R>
    int find_node(const R& s) const {
        int v = 0;
        for (auto c : as_range(s)) {
            v = nodes[v].next[index(c)];
            if (v == -1) return -1;
        }
        return v;
    }

   public:
    Trie() { add_node(-1, -1); }

    /**
     * @brief ノードの個数 (根を含む)
     */
    long long size() const { return nodes.size(); }

    /**
     * @brief 文字列 s を 1 個追加し、s に対応するノードを返す O(|s|)
     * @param s std::string や std::vector<int> など、要素を整数に変換できる列
     */
    template <typename R>
    long long insert(const R& s) {
        int v = 0;
        nodes[v].prefix_cnt++;
        for (auto c : as_range(s)) {
            v = extend_node(v, index(c));
            nodes[v].prefix_cnt++;
        }
        nodes[v].word_cnt++;
        return v;
    }

    /**
     * @brief 文字列 s を 1 個取り除く。s がなければ何もせず false を返す O(|s|)
     * ノードは削除しないので、ノードの番号は変わらない
     */
    template <typename R>
    bool erase(const R& s) {
        int v = find_node(s);
        if (v == -1 || nodes[v].word_cnt == 0) return false;
        nodes[v].word_cnt--;
        for (; v != -1; v = nodes[v].parent) nodes[v].prefix_cnt--;
        return true;
    }

    /**
     * @brief 文字列 s に対応するノード (なければ -1) O(|s|)
     * s を接頭辞に持つ文字列を追加したことがあれば、取り除いた後でもノードは残る
     */
    template <typename R>
    long long find(const R& s) const { return find_node(s); }

    /**
     * @brief 文字列 s の個数 O(|s|)
     */
    template <typename R>
    long long count(const R& s) const {
        int v = find_node(s);
        return v == -1 ? 0 : nodes[v].word_cnt;
    }
    /**
     * @brief s を接頭辞に持つ文字列の個数 O(|s|)
     */
    template <typename R>
    long long count_prefix(const R& s) const {
        int v = find_node(s);
        return v == -1 ? 0 : nodes[v].prefix_cnt;
    }
    /**
     * @brief 文字列の個数 (重複を含む)
     */
    long long total() const { return nodes[0].prefix_cnt; }

    /**
     * @brief ノード v から文字 c で進んだノード (なければ -1)
     */
    long long child(long long v, int c) const { return nodes[v].next[index(c)]; }
    /**
     * @brief ノード v から文字 c で進んだノード。なければ作る (文字列の個数は変えない) O(K)
     */
    long long extend(long long v, int c) { return extend_node(v, index(c)); }
    /**
     * @brief ノード v の親 (根なら -1)
     */
    long long parent(long long v) const { return nodes[v].parent; }
    /**
     * @brief ノード v の深さ (対応する文字列の長さ)
     */
    long long depth(long long v) const { return nodes[v].depth; }
    /**
     * @brief ノード v を通る (v の文字列を接頭辞に持つ) 文字列の個数
     */
    long long count_prefix_at(long long v) const { return nodes[v].prefix_cnt; }
    /**
     * @brief ノード v で終わる文字列の個数
     */
    long long count_at(long long v) const { return nodes[v].word_cnt; }

    /**
     * @brief ノード u, v の最小共通祖先 (2 つの文字列の最長共通接頭辞のノード) O(log n)
     */
    long long lca(long long u, long long v) const {
        int d = std::min(nodes[u].depth, nodes[v].depth);
        int a = ancestor(u, d), b = ancestor(v, d);
        if (a == b) return a;
        return nodes[below_lca(a, b).first].parent;
    }
    /**
     * @brief ノード u の文字列が v の文字列より辞書順で小さいか O(log n)
     * 一方が他方の接頭辞なら、短いほうが小さい
     */
    bool less(long long u, long long v) const {
        int d = std::min(nodes[u].depth, nodes[v].depth);
        int a = ancestor(u, d), b = ancestor(v, d);
        if (a == b) return nodes[u].depth < nodes[v].depth;
        auto [x, y] = below_lca(a, b);
        return nodes[x].ch < nodes[y].ch;
    }

    /**
     * @brief s の接頭辞のうち、追加した文字列のどれかの接頭辞にもなっているものの最長の長さ O(|s|)
     * 追加した文字列 (取り除いたものは除く) のどれかとの最長共通接頭辞の長さの最大値になる
     */
    template <typename R>
    long long longest_common_prefix(const R& s) const {
        int v = 0;
        long long len = 0;
        for (auto c : as_range(s)) {
            int u = nodes[v].next[index(c)];
            if (u == -1 || nodes[u].prefix_cnt == 0) break;
            v = u;
            len++;
        }
        return len;
    }
};
#line 2 "src/string/trie.hpp"

/**
 * @file trie.hpp
 * @brief Trie
 */

#include <algorithm>
#include <array>
#include <cassert>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>

/**
 * @brief Trie (文字列の集合を接頭辞の木で管理する)
 *
 * 文字 c は c - Base として 0 以上 K 未満の番号に直す。根はノード 0 (空文字列)
 * 同じ文字列を何度も追加でき、多重集合として扱う
 * ノードは jump pointer を持ち、2 つのノードの最小共通祖先や文字列の辞書順の比較が O(log n) でできる
 * 例:
 * Trie<26, 'a'> trie;
 * trie.insert("abc");
 * trie.count_prefix("ab");  // 1
 *
 * @tparam K 文字の種類数
 * @tparam Base 最小の文字
 */
template <int K = 26, int Base = 'a'>
class Trie {
    struct Node {
        std::array<int, K> next;
        int parent, depth;
        int ch;          // 親から来た辺の文字の番号 (根は -1)
        int jump;        // 祖先への飛び先 (深さの差が 1, 1, 3, 1, 1, 3, 7, ... となる skew-binary の飛び方)
        int prefix_cnt;  // このノードを通る (このノードの文字列を接頭辞に持つ) 文字列の個数
        int word_cnt;    // このノードで終わる文字列の個数
    };
    // メモリを抑えるため、ノードの中身は int で持つ。外へ返すときに long long にする
    std::vector<Node> nodes;

    int add_node(int parent, int ch) {
        Node node;
        node.next.fill(-1);
        node.parent = parent;
        node.ch = ch;
        node.prefix_cnt = node.word_cnt = 0;
        if (parent == -1) {
            node.depth = 0;
            node.jump = 0;
        } else {
            node.depth = nodes[parent].depth + 1;
            // 親の飛び先 2 つの深さの差が等しければ、2 つをまとめて飛ぶ
            int j = nodes[parent].jump, jj = nodes[j].jump;
            node.jump = nodes[parent].depth - nodes[j].depth == nodes[j].depth - nodes[jj].depth ? jj : parent;
        }
        nodes.push_back(node);
        return nodes.size() - 1;
    }
    // v の子 i (なければ作る)
    int extend_node(int v, int i) {
        if (nodes[v].next[i] == -1) {
            int u = add_node(v, i);
            nodes[v].next[i] = u;
        }
        return nodes[v].next[i];
    }
    // v の深さ d の祖先
    int ancestor(int v, int d) const {
        while (nodes[v].depth > d) v = nodes[nodes[v].jump].depth >= d ? nodes[v].jump : nodes[v].parent;
        return v;
    }
    // 深さが等しく異なる u, v を、親が一致する直前まで持ち上げる (飛び先は深さだけで決まるので、そろって飛べる)
    std::pair<int, int> below_lca(int u, int v) const {
        while (nodes[u].parent != nodes[v].parent) {
            if (nodes[u].jump != nodes[v].jump) {
                u = nodes[u].jump, v = nodes[v].jump;
            } else {
                u = nodes[u].parent, v = nodes[v].parent;
            }
        }
        return {u, v};
    }
    // 文字列リテラル ("abc" は char[4]) を末尾の '\0' まで読まないよう、文字列は string_view にする
    template <typename R>
    static decltype(auto) as_range(const R& s) {
        if constexpr (std::is_convertible_v<const R&, std::string_view>) {
            return std::string_view(s);
        } else {
            return (s);
        }
    }
    static int index(int c) {
        assert(0 <= c - Base && c - Base < K);
        return c - Base;
    }
    // 文字列 s に対応するノード (なければ -1)
    template <typename R>
    int find_node(const R& s) const {
        int v = 0;
        for (auto c : as_range(s)) {
            v = nodes[v].next[index(c)];
            if (v == -1) return -1;
        }
        return v;
    }

   public:
    Trie() { add_node(-1, -1); }

    /**
     * @brief ノードの個数 (根を含む)
     */
    long long size() const { return nodes.size(); }

    /**
     * @brief 文字列 s を 1 個追加し、s に対応するノードを返す O(|s|)
     * @param s std::string や std::vector<int> など、要素を整数に変換できる列
     */
    template <typename R>
    long long insert(const R& s) {
        int v = 0;
        nodes[v].prefix_cnt++;
        for (auto c : as_range(s)) {
            v = extend_node(v, index(c));
            nodes[v].prefix_cnt++;
        }
        nodes[v].word_cnt++;
        return v;
    }

    /**
     * @brief 文字列 s を 1 個取り除く。s がなければ何もせず false を返す O(|s|)
     * ノードは削除しないので、ノードの番号は変わらない
     */
    template <typename R>
    bool erase(const R& s) {
        int v = find_node(s);
        if (v == -1 || nodes[v].word_cnt == 0) return false;
        nodes[v].word_cnt--;
        for (; v != -1; v = nodes[v].parent) nodes[v].prefix_cnt--;
        return true;
    }

    /**
     * @brief 文字列 s に対応するノード (なければ -1) O(|s|)
     * s を接頭辞に持つ文字列を追加したことがあれば、取り除いた後でもノードは残る
     */
    template <typename R>
    long long find(const R& s) const { return find_node(s); }

    /**
     * @brief 文字列 s の個数 O(|s|)
     */
    template <typename R>
    long long count(const R& s) const {
        int v = find_node(s);
        return v == -1 ? 0 : nodes[v].word_cnt;
    }
    /**
     * @brief s を接頭辞に持つ文字列の個数 O(|s|)
     */
    template <typename R>
    long long count_prefix(const R& s) const {
        int v = find_node(s);
        return v == -1 ? 0 : nodes[v].prefix_cnt;
    }
    /**
     * @brief 文字列の個数 (重複を含む)
     */
    long long total() const { return nodes[0].prefix_cnt; }

    /**
     * @brief ノード v から文字 c で進んだノード (なければ -1)
     */
    long long child(long long v, int c) const { return nodes[v].next[index(c)]; }
    /**
     * @brief ノード v から文字 c で進んだノード。なければ作る (文字列の個数は変えない) O(K)
     */
    long long extend(long long v, int c) { return extend_node(v, index(c)); }
    /**
     * @brief ノード v の親 (根なら -1)
     */
    long long parent(long long v) const { return nodes[v].parent; }
    /**
     * @brief ノード v の深さ (対応する文字列の長さ)
     */
    long long depth(long long v) const { return nodes[v].depth; }
    /**
     * @brief ノード v を通る (v の文字列を接頭辞に持つ) 文字列の個数
     */
    long long count_prefix_at(long long v) const { return nodes[v].prefix_cnt; }
    /**
     * @brief ノード v で終わる文字列の個数
     */
    long long count_at(long long v) const { return nodes[v].word_cnt; }

    /**
     * @brief ノード u, v の最小共通祖先 (2 つの文字列の最長共通接頭辞のノード) O(log n)
     */
    long long lca(long long u, long long v) const {
        int d = std::min(nodes[u].depth, nodes[v].depth);
        int a = ancestor(u, d), b = ancestor(v, d);
        if (a == b) return a;
        return nodes[below_lca(a, b).first].parent;
    }
    /**
     * @brief ノード u の文字列が v の文字列より辞書順で小さいか O(log n)
     * 一方が他方の接頭辞なら、短いほうが小さい
     */
    bool less(long long u, long long v) const {
        int d = std::min(nodes[u].depth, nodes[v].depth);
        int a = ancestor(u, d), b = ancestor(v, d);
        if (a == b) return nodes[u].depth < nodes[v].depth;
        auto [x, y] = below_lca(a, b);
        return nodes[x].ch < nodes[y].ch;
    }

    /**
     * @brief s の接頭辞のうち、追加した文字列のどれかの接頭辞にもなっているものの最長の長さ O(|s|)
     * 追加した文字列 (取り除いたものは除く) のどれかとの最長共通接頭辞の長さの最大値になる
     */
    template <typename R>
    long long longest_common_prefix(const R& s) const {
        int v = 0;
        long long len = 0;
        for (auto c : as_range(s)) {
            int u = nodes[v].next[index(c)];
            if (u == -1 || nodes[u].prefix_cnt == 0) break;
            v = u;
            len++;
        }
        return len;
    }
};
Back to top page