HDU 4787GRE Words Revenge

来源:互联网 发布:摄影师作品网站知乎 编辑:程序博客网 时间:2024/06/03 21:34

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 <= 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
 

Source
2013 Asia Chengdu Regional Contest
想法:离线AC自动机
代码:
#include<queue>#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;#define sz 2#define N 5000010char 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;}


原创粉丝点击