HDU 4787GRE Words Revenge

GRE Words Revenge

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)
Total Submission(s): 2578    Accepted Submission(s): 631


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
 

Source
想法:离线AC自动机
代码:
#include<queue>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define sz 2
#define N 5000010
char str[N], s1[N];
struct Node
{
    Node *fail, *nxt[sz];
    long long  cnt;
    Node()
    {
        fail=NULL, cnt=0;
        for(int i=0; i<sz; i++)
            nxt[i]=NULL;
    }
}*root;
void insert( char *s )
{
    int tmp, len=strlen(s);
    Node *p=root;
    for ( int i=0; i<len; i++)
    {
        tmp=s[i]-'0';
        if( p->nxt[tmp]==NULL )
            p->nxt[tmp]=new Node();
        p=p->nxt[tmp];
    }
    p->cnt++;
}
long long find(char *str)
{
    int tmp, len=strlen(str);long long ans=0;
    Node *p=root;
    for (int i=0; i<len; i++)
    {
        tmp=str[i]-'0';
        while( p->nxt[tmp]==NULL&&p!=root) p=p->fail;
        p=p->nxt[tmp];
        if(p==NULL) p=root;
        Node *q=p;
        while( q!=root)
            ans+=q->cnt, q=q->fail;
    }
    return ans;
}
void build_Fail()
{
    queue<Node *>q;
    q.push(root);
    while(!q.empty())
    {
        Node *p=q.front(), *tmp; q.pop();
        for ( int i=0; i<sz; i++ )
            if( p->nxt[i]!=NULL )
            {
               if( p==root ) p->nxt[i]->fail=root;
               else
               {
                  tmp=p->fail;
                  while( tmp!=NULL )
                  {
                      if( tmp->nxt[i]!=NULL )
                      {
                          p->nxt[i]->fail=tmp->nxt[i];
                          break;
                      }
                       tmp=tmp->fail;
                  }
                  if( tmp==NULL )
                    p->nxt[i]->fail=root;
               }
               q.push(p->nxt[i]);
            }
    }
}
void shift(char *str,long long num)
{
    int len=strlen(str);
    num%=len;
    num=len-num;
    if(!num)
        return;
    int i=0;
    for(i=0;i<num;i++)
    {
        s1[i]=str[len-num+i];
    }
    for(i=num;i<len;i++)
    {
        s1[i]=str[i-num];
    }
    for(i=0;i<len;i++)
    {
        str[i]=s1[i];
    }
}
int seach(char *s)//判重
{
    int len=strlen(s),i,j,now;
    Node *cur=root;
    for(i=0;i<len;i++)
    {
        now=s[i]-'0';
        if(cur->nxt[now]!=NULL)
        {
            cur=cur->nxt[now];
        }
        else
            return 0;
    }
    if(cur->cnt)
        return 1;
    return 0;
}
void del(Node *root)
{
    for(int i=0;i<sz;i++)
      if(root->nxt[i]!=NULL)
        del(root->nxt[i]);
    free(root);
}
int main()
{
    int t,c=0;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        root=new Node();
        scanf("%d",&n);
        printf("Case #%d:\n",++c);
        int i;
        long long num=0;
        for(i=0;i<n;i++)
        {
            scanf("%s",str);
            if(str[0]=='+')
            {
                shift(str+1,num);
                if(seach(str+1))
                    continue;
                insert(str+1);
            }
            else
            {
                shift(str+1,num);
                build_Fail();
                long long ans=find(str+1);
                num=ans;
                printf("%lld\n",ans);
           }
        }
        del(root);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值