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

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 <= 10 5), 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 s 1s 2 ... s k is s ks 1s 2 ... s k-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 10 5 and total length of the paragraphs does not exceed 5 * 10 6.
 

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
  
  
2 3 +01 +01 ?01001 3 +01 ?010 ?011
 

Sample Output
  
  
Case #1: 2 Case #2: 1 0
思路:如果是离线的话这题就是模板了。。。在线的话是开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 int
int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值