HDU4787--GRE Words Revenge(在线AC自动机)

来源:互联网 发布:linux locale命令i18n 编辑:程序博客网 时间:2024/05/21 02:53
Problem Description
  Now Coach Pang is preparing for the Graduate Record Examinations as George did in 2011. At each day, Coach Pang can:
  "+w": learn a word w
  "?p": read a paragraph p, and count the number of learnt words. Formally speaking, count the number of substrings of p which is a learnt words.
  Given the records of N days, help Coach Pang to find the count. For convenience, the characters occured in the words and paragraphs are only '0' and '1'.
 

Input
  The first line of the input file contains an integer T, which denotes the number of test cases. T test cases follow.
  The first line of each test case contains an integer N (1 <= N <= 105), which is the number of days. Each of the following N lines contains either "+w" or "?p". Both p and w are 01-string in this problem.
  Note that the input file has been encrypted. For each string occured, let L be the result of last "?" operation. The string given to you has been shifted L times (the shifted version of string s1s2 ... sk is sks1s2 ... sk-1). You should decrypt the string to the original one before you process it. Note that L equals to 0 at the beginning of each test case.
  The test data guarantees that for each test case, total length of the words does not exceed 105 and total length of the paragraphs does not exceed 5 * 106.
 

Output
  For each test case, first output a line "Case #x:", where x is the case number (starting from 1).
  And for each "?" operation, output a line containing the result.
 

Sample Input
23+01+01?010013+01?010?011
 

Sample Output
Case #1:2Case #2:10
思路:如果是离线的话这题就是模板了。。。在线的话是开2个AC自动机、因为每次插入一个单词都要重建失败指针,当第二个AC自动机规模较大的时候往第一个合并。
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;#pragma comment(linker, "/STACK:1024000000,1024000000")  #define maxn 2008000#define maxm 8000800#define LL long long intint first,rear;char s1[maxm];char str[maxm];int node_num;struct node{    node * fail;    node * next[2];    LL key,vis;    node()    {        key = vis = 0;        fail = NULL;        for(int i = 0;i < 2;i++)            next[i] = NULL;    }}*q[maxm];void Creat_Trie(char * s,node * root){    node * p = root;    int len = strlen(s);    for(int i = 0;i < len;i++)    {        int id = s[i] - '0';        if(p -> next[id] == NULL)        {            node_num++;            p -> next[id] = new node();        }        p = p -> next[id];    }p -> vis = 1;}void build_ac_automation(node * root){    first = rear = 0;    node * p = NULL;    node * temp = NULL;    root -> fail = NULL;    q[rear++] = root;    while(first < rear)    {        p = q[first++];        for(int i = 0;i < 2;i++)        {            if(p -> next[i] != NULL)            {                if(p == root){                    p -> next[i] -> fail = root;p -> next[i] -> key = p -> next[i] -> vis;}                else                 {                    temp = p -> fail;                    while(temp != NULL)                    {                        if(temp -> next[i] != NULL)                        {                            p -> next[i] -> fail = temp -> next[i];                            p -> next[i] -> key = temp -> next[i] -> key + p -> next[i] -> vis;                            break;                        }                        temp = temp -> fail;                    }                    if(temp == NULL){                        p -> next[i] -> fail = root;p -> next[i] -> key = p -> next[i] -> vis;}                }                q[rear++] = p -> next[i];            }        }            }}int search(char * s,node * root){    int len = strlen(s);    node * p = root;    for(int i = 0;i < len;i++)    {        int id = s[i] - '0';        p = p -> next[id];        if(p == NULL)return 0;    }    if(p -> vis)    return 1;    return 0;}LL query(char * s,node * root){    int len = strlen(s);    node * p = root;    LL ans = 0;    for(int i = 0;i < len;i++)    {        int id = s[i] -'0';        while(p -> next[id] == NULL && p != root)            p = p -> fail;        p = p -> next[id];        if(p == NULL) p = root;        ans += p -> key;    }    return ans;}void Shift(char * s,LL l)///////////////////////////////{    LL len = strlen(s);    l %= len;    l = len-l;    if(!l)    return;    for(LL i = 0;i < l;i++)        s1[i] = s[len-l+i];    for(LL i = l;i < len;i++)        s1[i] = s[i-l];    for(LL i = 0;i < len;i++)        s[i] = s1[i];    s[len] = '\0';}queue <node *> Q;void Union1(node * root1,node * root2)///////////////////{first = rear = 0;    q[rear++] = root1;q[rear++] = root2;    while(first < rear)    {        node * r1 = q[first++];        node * r2 = q[first++];        for(int i = 0;i < 2;i++)        {            if(r2 -> next[i] != NULL)            {                if(r1 -> next[i] == NULL)                {                    r1 -> next[i] = new node();                }r1 -> next[i] -> vis |= r2 -> next[i] -> vis;                q[rear++] = r1 -> next[i];                q[rear++] = r2 -> next[i];            }        }    }}void Delete(node * root)/////////////////////////{    if(root == NULL)    return;    for(int i = 0;i < 2;i++)        if(root -> next[i] != NULL)            Delete(root -> next[i]);    delete(root);}int main(){//freopen("in.txt","r",stdin);    int t,cas = 0;        scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        node * root1 = new node(),* root2 = new node();        LL Shit = 0;        node_num = 0;        printf("Case #%d:\n",++cas);        build_ac_automation(root1);        for(int i = 1;i <= n;i++)        {            scanf("%s",str);            if(str[0] == '?')            {                Shift(str+1,Shit);                Shit = query(str+1,root1) + query(str+1,root2);                printf("%I64d\n",Shit);            }            else             {                Shift(str+1,Shit);                if(search(str+1,root1) || search(str+1,root2))    continue;                Creat_Trie(str+1,root2);build_ac_automation(root2);                if(node_num > 2000)                {                    Union1(root1,root2);                    Delete(root2);                    root2 = new node();                    build_ac_automation(root1);                    build_ac_automation(root2);                    node_num = 0;                }            }        }        Delete(root1);        Delete(root2);    }    return 0;}


0 0
原创粉丝点击