Bzoj 2434 NOI 2011 The typewriter of a beaver AC automatic mechanism built fail tree

Source: Internet
Author: User

The main question: there is a typewriter with 28 letters, respectively, 26 lowercase and BP, where B stands for backspace, p for newline, and each line is a string. Now label these strings and ask the X string to appear several times in the Y string.


Thought: This is the NOI history of the most difficult string of the problem ( Zoo ).

First of all, according to test instructions not difficult to build an AC automaton out, according to the normal thinking, for each inquiry is required in the AC automaton on the search for violence. But this time will be very good-looking.

So we think that the fail pointer must be a tree, and the DFS sequence of this tree will have a very good nature--the string in which there is a string must be in the subtree of the string, which corresponds to a section of the DFS sequence. Let's do it. All queries are offline, a deep search of the entire trie tree, and a Fenwick to maintain the DFS sequence, and the way to deal with the answer.


CODE:

#include <queue> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <iostream> #include <algorithm> #define MAX 100010using namespace std; #define P (a) ((a)-' a ') struct T    rie{Trie *son[26],*fail;         int end;        Trie () {memset (son,null,sizeof (son));    Fail = NULL; }}mempool[max],*c = Mempool + 1,*root = c + +; int asks; int fenwick[max];int Head[max],total;int Next[max],aim[max]; int Pos[max],p[max];p air<int,int> range[max];vector<pair<int,int> > G[max];int Ans[MAX];    inline void Add (int x,int y) {next[++total] = head[x];    Aim[total] = y; HEAD[X] = total;}    void Buildtrie () {static Trie *stack[max],*now = root;    int top = 0,cnt = 0;    char c;    Stack[++top] = root;            while (c = GetChar (), Isalpha (c)) {if (c = = ' P ') {now->end = ++cnt;        P[now-mempool] = cnt;        } else if (c = = ' B ') now = stack[top--];   else {         Stack[++top] = now;            if (Now->son[p (c)] = = NULL) now->son[p (c)] = C + +;                   now = Now->son[p (c)];    }}} void Buildacautomation () {static Queue<trie *> q;    while (!q.empty ()) Q.pop ();    Q.push (root);        while (!q.empty ()) {Trie *now = Q.front (); Q.pop ();            for (int i = 0; i <; ++i) {if (now->son[i] = = NULL) continue;                if (now = = root) {now->son[i]->fail = root;            ADD (Root-mempool,now->son[i]-mempool);                } else {Trie *temp = now->fail;                while (temp! = root && temp->son[i] = = NULL) temp = temp->fail;                if (temp->son[i]! = NULL) temp = temp->son[i];                Now->son[i]->fail = temp;            ADD (Temp-mempool,now->son[i]-mempool);        } q.push (Now->son[i]); }}} void Pre (int x) {static INT k = 0;    POS[X] = ++k;    if (p[x]) Range[p[x]].first = k;    for (int i = head[x]; i; i = Next[i]) Pre (Aim[i]); if (p[x]) Range[p[x]].second = k;} inline void Fix (int x,int c) {for (; x < MAX; x + = X&-x) fenwick[x] + = c;} inline int getsum (int x) {in    t re = 0;    for (; x; x = x&-x) re +=fenwick[x]; return re;}    void DFS (int x) {Trie *now = &mempool[x];    if (Pos[x]) Fix (pos[x],1); For (vector<pair<int,int> >::iterator it = G[p[x]].begin (); It! = G[p[x]].end (); ++it) {Pair<int,int        > now = *it;    Ans[now.second] = getsum (range[now.first].second)-getsum (range[now.first].first-1);    } for (int i = 0; i <; ++i) if (now->son[i]! = NULL) DFS (Now->son[i]-mempool); if (Pos[x]) Fix (pos[x],-1);}    int main () {Buildtrie ();    Buildacautomation ();    Cin >> asks;        for (int x,y,i = 1; I <= asks; ++i) {scanf ("%d%d", &x,&y); G[y].push_back (Make_pair (x,i));    } Pre (1);    DFS (1);    for (int i = 1; I <= asks; ++i) printf ("%d\n", Ans[i]); return 0;}?


Bzoj 2434 NOI 2011 The typewriter of a beaver AC automatic mechanism built fail tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.