灌水区dalao多 && 求条P3966
  • 板块灌水区
  • 楼主mm1214
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/4/26 23:10
  • 上次更新2024/4/27 10:28:53
查看原帖
灌水区dalao多 && 求条P3966
774258
mm1214楼主2024/4/26 23:10
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n; string t[205];
struct AC {
    struct ACnode {
        int son[26] , fail , end;
    } tr[5000005]; int tot;
    int cnt[5000005] , a[100005];
    void insert(const string &s , int x) {
        int p = 0;
        for (int i = 0; i < s.size(); i++) {
            if (!tr[p].son[s[i] - 'a']) tr[p].son[s[i] - 'a'] = ++tot;
            p = tr[p].son[s[i] - 'a'];
            cnt[p]++;
        }
        tr[p].end++;
        a[x] = p;
    }
    void getfail() {
        queue <int> q;
        for (int i = 0; i < 26; i++) {
            if (tr[0].son[i]) {
                q.push(tr[0].son[i]);
                tr[tr[0].son[i]].fail = 0;
            }
        }
        int u;
        while(!q.empty()) {
            u = q.front(); q.pop();
            for (int i = 0; i < 26; i++) {
                if (tr[u].son[i]) {
                    tr[tr[u].son[i]].fail = tr[tr[u].fail].son[i];
                    q.push(tr[u].son[i]);
                } else {
                    tr[u].son[i] = tr[tr[u].fail].son[i];
                }
            }
        }
    }
    void query() {
        for (int i = tot; i >= 0; i--) cnt[tr[i].fail] += cnt[i];
        for (int i = 1; i <= n; i++) cout << cnt[a[i]] << '\n';
    }
} ac;
signed main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> t[i] , ac.insert(t[i] , i);
    ac.getfail();
    ac.query();
    return 0;
}

记录

悬小号一关

2024/4/26 23:10
加载中...