AC自动机 代码记录

· · 算法·理论

//Ciallo~(∠・ω< )⌒★

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
inline ll read() {
    ll x = 0, flag = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') flag = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch ^ 48);
        ch = getchar();
    }
        return x * flag;
}
void out(ll x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x < 10)
        putchar(x + '0');
    else {
        out(x / 10);
        putchar(x % 10 + '0');
    }
}

const int N = 2e5 + 5;
const int ALPHA = 26;

struct AC_Automaton {
    int tr[N][ALPHA], fail[N], idx;
    vector<int> id[N]; 
    unordered_map<string, vector<int>> mp; 
    vector<int> ans;  

    void init(int n) {
        memset(tr[0], 0, sizeof(tr[0]));
        fail[0] = 0;
        idx = 0;
        mp.clear();
        ans.assign(n + 1, 0);  
    }

    void insert(const string &s, int pid) {
        mp[s].push_back(pid); 
        int p = 0;
        for (char ch : s) {
            int c = ch - 'a';
            if (!tr[p][c]) {
                tr[p][c] = ++idx;
                memset(tr[idx], 0, sizeof(tr[idx]));
                id[idx].clear();
            }
            p = tr[p][c];
        }
        id[p].push_back(pid); 
    }

    void build() {
        queue<int> q;
        for (int i = 0; i < ALPHA; i++) 
            if (tr[0][i]) {
                fail[tr[0][i]] = 0;
                q.push(tr[0][i]);
            }
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (int i = 0; i < ALPHA; i++) {
                if (tr[u][i]) {
                    fail[tr[u][i]] = tr[fail[u]][i];
                    q.push(tr[u][i]);
                } else tr[u][i] = tr[fail[u]][i];
            }
        }
    }

    void query(const string &s) {
        vector<int> vis(idx + 1, 0);
        int p = 0;
        for (char ch : s) {
            p = tr[p][ch - 'a'];
            vis[p]++;
        }
        vector<int> in(idx + 1, 0);
        queue<int> q;
        for (int i = 1; i <= idx; i++) in[fail[i]]++;
        for (int i = 1; i <= idx; i++) if (in[i] == 0) q.push(i);
        while (!q.empty()) {
            int u = q.front(); q.pop();
            for (int pid : id[u]) ans[pid] += vis[u]; 
            int v = fail[u];
            vis[v] += vis[u];
            if (--in[v] == 0) q.push(v);
        }
        for (auto it = mp.begin(); it != mp.end(); ++it) {
            vector<int> ids = it->second;
            if (ids.size() > 1) {
                int cnt = ans[ids[0]];
                for (int pid : ids) ans[pid] = cnt;
            }
        }
    }
} AC;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int n;
    cin >> n;
    AC.init(n);

    for (int i = 1; i <= n; i++) {
        string s;
        cin >> s;
        AC.insert(s, i);
    }

    AC.build();
    string t;
    cin >> t;
    AC.query(t);

    for (int i = 1; i <= n; i++) 
        cout << AC.ans[i] << '\n';

    return 0;
}