cp-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub rniya/cp-library

:heavy_check_mark: Aho Corasick
(src/string/AhoCorasick.hpp)

概要

各パターン文字列を区別する必要がない場合には build() の引数 heavyfalse にする.

Depends on

Verified with

Code

#pragma once
#include <algorithm>
#include <map>
#include <queue>
#include "Trie.hpp"

template <size_t char_size, char margin = 'a'> struct AhoCorasick : Trie<char_size + 1, margin> {
    void build(bool heavy = true) {
        int n = nodes.size();
        cnt.resize(n);
        for (int i = 0; i < n; i++) cnt[i] = nodes[i].idxs.size();
        std::queue<int> que;
        for (size_t i = 0; i <= char_size; i++) {
            if (~next(0, i)) {
                next(next(0, i), FAIL) = 0;
                que.emplace(next(0, i));
            } else
                next(0, i) = 0;
        }
        while (!que.empty()) {
            auto& cur = nodes[que.front()];
            int fail = cur.nxt[FAIL];
            cnt[que.front()] += cnt[fail];
            que.pop();
            for (size_t i = 0; i < char_size; i++) {
                int& nxt = cur.nxt[i];
                if (nxt < 0) {
                    nxt = next(fail, i);
                    continue;
                }
                next(nxt, FAIL) = next(fail, i);
                if (heavy) {
                    auto& u = nodes[nxt].idxs;
                    auto& v = nodes[next(fail, i)].idxs;
                    std::vector<int> w;
                    std::set_union(u.begin(), u.end(), v.begin(), v.end(), back_inserter(w));
                    u = w;
                }
                que.emplace(nxt);
            }
        }
    }

    long long match(const std::string& s) {
        long long res = 0;
        int cur = 0;
        for (const char& c : s) {
            cur = next(cur, c - margin);
            res += cnt[cur];
        }
        return res;
    }

    std::map<int, int> frequency(const std::string& s) {
        std::map<int, int> res;
        int cur = 0;
        for (const char& c : s) {
            cur = next(cur, c - margin);
            for (auto& idx : nodes[cur].idxs) res[idx]++;
        }
        return res;
    }

    int count(int pos) { return cnt[pos]; }

private:
    using super = Trie<char_size + 1, margin>;
    using super::next;
    using super::nodes;

    const int FAIL = char_size;
    std::vector<int> cnt;
};
#line 2 "src/string/AhoCorasick.hpp"
#include <algorithm>
#include <map>
#include <queue>
#line 3 "src/string/Trie.hpp"
#include <array>
#include <cassert>
#include <string>
#include <vector>

template <size_t char_size, char margin = 'a'> struct Trie {
    struct Node {
        std::array<int, char_size> nxt;
        std::vector<int> idxs;
        int idx, sub;
        char key;
        Node(char c) : idx(-1), sub(0), key(c) { std::fill(nxt.begin(), nxt.end(), -1); }
    };

    std::vector<Node> nodes;

    inline int& next(int i, int j) { return nodes[i].nxt[j]; }

    Trie() { nodes.emplace_back('$'); }

    void add(const std::string& s, int x = 0) {
        int cur = 0;
        for (const char& c : s) {
            int k = c - margin;
            if (next(cur, k) < 0) {
                next(cur, k) = nodes.size();
                nodes.emplace_back(c);
            }
            cur = next(cur, k);
            nodes[cur].sub++;
        }
        nodes[cur].idx = x;
        nodes[cur].idxs.emplace_back(x);
    }

    int find(const std::string& s) {
        int cur = 0;
        for (const char& c : s) {
            int k = c - margin;
            if (next(cur, k) < 0) return -1;
            cur = next(cur, k);
        }
        return cur;
    }

    int move(int pos, char c) {
        assert(pos < (int)nodes.size());
        return pos < 0 ? -1 : next(pos, c - margin);
    }

    int size() const { return nodes.size(); }

    int idx(int pos) { return pos < 0 ? -1 : nodes[pos].idx; }

    std::vector<int> idxs(int pos) { return pos < 0 ? std::vector<int>() : nodes[pos].idxs; }
};
#line 6 "src/string/AhoCorasick.hpp"

template <size_t char_size, char margin = 'a'> struct AhoCorasick : Trie<char_size + 1, margin> {
    void build(bool heavy = true) {
        int n = nodes.size();
        cnt.resize(n);
        for (int i = 0; i < n; i++) cnt[i] = nodes[i].idxs.size();
        std::queue<int> que;
        for (size_t i = 0; i <= char_size; i++) {
            if (~next(0, i)) {
                next(next(0, i), FAIL) = 0;
                que.emplace(next(0, i));
            } else
                next(0, i) = 0;
        }
        while (!que.empty()) {
            auto& cur = nodes[que.front()];
            int fail = cur.nxt[FAIL];
            cnt[que.front()] += cnt[fail];
            que.pop();
            for (size_t i = 0; i < char_size; i++) {
                int& nxt = cur.nxt[i];
                if (nxt < 0) {
                    nxt = next(fail, i);
                    continue;
                }
                next(nxt, FAIL) = next(fail, i);
                if (heavy) {
                    auto& u = nodes[nxt].idxs;
                    auto& v = nodes[next(fail, i)].idxs;
                    std::vector<int> w;
                    std::set_union(u.begin(), u.end(), v.begin(), v.end(), back_inserter(w));
                    u = w;
                }
                que.emplace(nxt);
            }
        }
    }

    long long match(const std::string& s) {
        long long res = 0;
        int cur = 0;
        for (const char& c : s) {
            cur = next(cur, c - margin);
            res += cnt[cur];
        }
        return res;
    }

    std::map<int, int> frequency(const std::string& s) {
        std::map<int, int> res;
        int cur = 0;
        for (const char& c : s) {
            cur = next(cur, c - margin);
            for (auto& idx : nodes[cur].idxs) res[idx]++;
        }
        return res;
    }

    int count(int pos) { return cnt[pos]; }

private:
    using super = Trie<char_size + 1, margin>;
    using super::next;
    using super::nodes;

    const int FAIL = char_size;
    std::vector<int> cnt;
};
Back to top page