CSES Counting Patterns
// compile: make data
// run: ./data < data.in
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#include <debug/codeforces.h>
#define debug(x...) _debug_print(#x, x);
#define Debug(x...) _debug_print_format(#x, x);
std::ifstream terminal("/dev/tty");
#define PP cerr<<"\033[1;30mpause...\e[0m",terminal.ignore();
#else
#define debug(x...)
#define Debug(x...)
#define PP
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define VI vector<int>
#define VII vector<vector<int>>
#define VIII vector<vector<vector<int>>>
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define sz(v) ((int)(v).size())
#define print(...) print_(__VA_ARGS__);
#define FIND(a, x) ((find(a.begin(),a.end(),(x))!=a.end())?1:0)
#define cmin(x,...) x=min({(x), __VA_ARGS__})
#define cmax(x,...) x=max({(x), __VA_ARGS__})
#define INTMAX (int)(9223372036854775807)
#define INF (int)(1152921504606846976)
#define NaN (int)(0x8b88e1d0595d51d1)
#define double long double
#define int long long
#define uint unsigned long long
#define MAXN 200010

struct acauto {
    int base, N;
    struct node {
        int isword, c, depth;
        vector<node*> ch;
        vector<int> id;
        node *parent = nullptr;
        node *fail = nullptr, *dict = nullptr;
        int fst = -1, cnt = 0;
    };
    node *root;
    vector<node> datalist;
    int total = 0;
    node *newnode(int c, node *p = nullptr) {
        node *x = &datalist[total++];
        x->ch.resize(N, 0);
        x->c = c;
        x->parent = p;
        return x;
    }
    acauto(int M, char L = 0, char R = 127) {
        base = L;
        N = R - L + 1;
        datalist.resize(M);
        root = newnode(-1);
    }
    void insert(string s, int i) {
        node *x = root;
        for (char c: s) {
            if (!x->ch[c - base]) {
                x->ch[c - base] = newnode(c - base, x);
            }
            x = x->ch[c - base];
        }
        x->isword = 1;
        x->id.push_back(i);
    }
    void build() {
        root->depth = 0;
        root->fail = root;
        root->dict = root->isword ? root : nullptr;
        queue<node*> q;
        rep(i, 0, N) if (root->ch[i]) {
            root->ch[i]->fail = root;
            root->ch[i]->dict = root->dict;
            root->ch[i]->depth = root->depth + 1;
            q.push(root->ch[i]);
        }
        while (!q.empty()) {
            node *x = q.front(); q.pop();
            rep(i, 0, N) if (x->ch[i]) {
                node *y = x->fail;
                while (y != root && !y->ch[i]) y = y->fail;
                x->ch[i]->depth = x->depth + 1;
                x->ch[i]->fail = y->ch[i] ? y->ch[i] : root;
                x->ch[i]->dict = x->ch[i]->fail->isword ? x->ch[i]->fail : x->ch[i]->fail->dict;
                q.push(x->ch[i]);
            }
        }
    }
    void match(string s) {
        auto found = [&](node *x, int i) {
            if (!~x->fst) x->fst = i - x->depth + 1;
            ++x->cnt;
        };
        node *x = root;
        rep(i, 0, s.length()) {
            while (x != root && !x->ch[s[i] - base]) x = x->fail;
            if (x->ch[s[i] - base]) x = x->ch[s[i] - base];
            if (x->isword) found(x, i);
            for (node *tmp = x; tmp->dict; tmp = tmp->dict) found(tmp->dict, i);
        }
    }
    string get(node *x) {
        vector<char> v;
        while (x->parent) {
            v.push_back(x->c + base);
            x = x->parent;
        }
        reverse(v.begin(), v.end());
        string res(v.begin(), v.end());
        return res;
    }
    void graphviz_dump(string filename = "graph.dot") {
        FILE *f = fopen(filename.c_str(), "w");
        fprintf(f, "digraph {\n");
        rep(i, 0, total) {
            node *x = &datalist[i];
            fprintf(f, "    %lld;\n", i, (x->c >= 0 ? char(x->c + base) : ' '), x->isword ? "red" : "black");
        }
        queue<node*> q; q.push(root);
        while (!q.empty()) {
            node *x = q.front(); q.pop();
            rep(i, 0, N) {
                if (x->ch[i]) {
                    fprintf(f, "    %ld -> %ld;\n", x - &datalist[0], x->ch[i] - &datalist[0]);
                    q.push(x->ch[i]);
                }
            }
            if (x->fail != root) fprintf(f, "    %ld -> %ld[style=\"dashed\" color=\"blue\"];\n", x - &datalist[0], x->fail - &datalist[0]);
            if (x->dict) fprintf(f, "    %ld -> %ld[style=\"dashed\" color=\"green\"];\n", x - &datalist[0], x->dict - &datalist[0]);
        }
        fprintf(f, "}\n");
    }
};

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    string T; cin >> T;
    int n; cin >> n;
    acauto ac(1e6+1, 'a', 'z');
    rep(i, 0, n) {
        string s; cin >> s;
        ac.insert(s, i);
    }
    ac.build();
    ac.match(T);
    vector<int> ans(n, 0);
    rep(i, 0, ac.total) {
        auto *x = &ac.datalist[i];
        for (int id: x->id) ans[id] = x->cnt;
    }
    rep(i, 0, n) cout << ans[i] << endl;

    return 0;
}
No Comments

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
Previous
Next