Luo Gu -P5357- [template] AC automaton (secondary enhanced version)

Topic Portal

-------------------------------------- New Year at home bored mend this week to do a few AC automatic machine template title

Sol: AC automaton, must be resolved repeat visits jump fail to produce the side, but this time with the last edge has to die, only to get 76 points. We put the process into a one-time jump fail sides were scanned after the string.

  • AC automaton
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> PII;
    const int MAXN = 200010;
    struct Trie {
        int son[MAXN][26], fail[MAXN];
        int que[MAXN]; int head, tail;
        int cnt[MAXN]; int tot, root;
        int add_node() {
            memset(son[tot], -1, sizeof(son[tot]));
            cnt[tot] = 0;
            return tot ++;
        }
        void init() {
            head = tail = 0;
            tot = 0;
            root = add_node();
        }
        int insert(char* s) {
            int p = root;
            for (int i = 0; s[i]; i++) {
                int index = s[i] - 'a';
                if (son[p][index] == -1)
                    son[p][index] = add_node();
                p = son[p][index];
            }
            return p;
        }
        void build() {
            fail[root] = root;
            for (int i = 0; i < 26; i++) {
                if (son[root][i] == -1) son[root][i] = root;
                else {
                    fail[son[root][i]] = root;
                    que[++ tail] = son[root][i];
                }
            }
            while (head != tail) {
                int p = que[++ head];
                for (int i = 0; i < 26; i++) {
                    if (son[p][i] == -1) son[p][i] = son[fail[p]][i];
                    else {
                        fail[son[p][i]] = son[fail[p]][i];
                        que[++ tail] = son[p][i];
                    }
                }
            }
        }
        void slove(char* s) {
            int p = root;
            for (int i = 0; s[i]; i++) {
                int index = s[i] - 'a';
                p = son[p][index];
                cnt[p] ++;
            }
            for (int i = tail; i >= 1; i--) {
                int p = que[i];
                cnt[fail[p]] += cnt[p];
            }
        }
    } ac;
    intInde [MAXN]; // run locally no problem, they reported the Los valley called the index compilation errors 
    char S [ 2.00001 million ];
     int main () {
         int the n-; ac.init (); 
        scanf ( " % d " , & the n-) ;
         for ( int I = . 1 ; I <= n-; I ++ ) { 
            Scanf ( " % S " , S); 
            Inde [I] = ac.insert (S); 
        } 
        ac.build (); 
        Scanf ( " % S " , S); ac.slove (S);
         for ( int I = . 1 ; I <= n-; I ++)
            printf("%d\n", ac.cnt[inde[i]]);
        return 0;
    }

    After continuous 40 points, 60 points, 76 points, finally get. For the last time in jumping fail to ensure the higher side of the depth of the node the first jump the queue using handwriting reserved hierarchy bfs time. Line to see someone else's code is said to be used topology, did not look. But the feeling here was a queue depth of the topological sort.

Guess you like

Origin www.cnblogs.com/Angel-Demon/p/12233327.html