• P3966 [TJOI2013]单词


    题意:给n个单词,问每个单词在n个单词中出现了几次?  a 在 acdnsaana 中出现了4次。

    ps:AC自动机的模板题,就注意一点:一定要从这棵树的底部向上遍历累加贡献。

    const int N = 1000010;
    
    struct node {
        int fail, Count;
        int vis[26];
        node() {
            mem(vis, 0);
            fail = Count = 0;
        }
    };
    
    struct AcToMation {
        node a[N];
        int tot, sum[N], id[300];
    
        stack<int> st;
    
        void Inite() {
            tot = 0;
            mem(sum, 0);
        }
        void Build(char *s, int t) {
            int n = strlen(s);
            int now = 0;
            rep(i, 0, n) {
                if (!a[now].vis[s[i] - 'a']) a[now].vis[s[i] - 'a'] = ++tot;
                now = a[now].vis[s[i] - 'a'];
                sum[now]++;
            }
            a[now].Count++;
            id[t] = now;
        }
        void getFail() {
            queue<int> q;
            rep(i, 0, 26) if (a[0].vis[i]) {
                a[a[0].vis[i]].fail = 0;
                q.push(a[0].vis[i]);
            }
            while(!q.empty()) {
                int now = q.front();
                q.pop();
                st.push(now);
                rep(i, 0, 26) {
                    if (a[now].vis[i]) {
                        a[a[now].vis[i]].fail = a[a[now].fail].vis[i];
                        q.push(a[now].vis[i]);
                    }
                    else {
                        a[now].vis[i] = a[a[now].fail].vis[i];
                    }
                }
            }
        }
        void Compute(int n) {
            while(!st.empty()) {
                sum[a[st.top()].fail] += sum[st.top()];
                st.pop();
            }
            Rep(i, 1, n) pr(sum[id[i]]);
        }
    };
    
    AcToMation ac;
    
    
    int main()
    {
        ac.Inite();
    
        int n;
        char s[N];
    
        sc(n);
        Rep(i, 1, n) {
            scanf("%s", s);
            ac.Build(s, i);
        }
        ac.getFail();
        ac.Compute(n);
        return 0;
    }
  • 相关阅读:
    vue 开发系列 企业微信整合
    MongoDB基础3
    MongoDB基础2
    MongoDB基础1
    SpringBoot MongoDB
    UWSGI
    Nginx
    编译python源码
    Flask部署
    Django个人工作总结
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/9710769.html
Copyright © 2020-2023  润新知