Hdoj topic 4787 GRE Words Revenge (online ac automata, offline also can do)

Source: Internet
Author: User

GRE Words RevengeTime limit:20000/10000 MS (java/others) Memory limit:327680/327680 K (java/others)
Total submission (s): 1570 Accepted Submission (s): 352


Problem Description now Coach Pang are preparing for the graduate Record examinations as George do 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 coaches Pang to find the count. For convenience, the characters occured in the words and paragraphs is only ' 0 ' and ' 1 '.
Input the first line of the input file contains an integer T, which denotes the number of the test cases. T test Cases follow.
The first line of all test case contains a integer n (1 <= n <=), which is the number of days. Each of the following N lines contains either "+w" or "? P". Both P and W is 01-string in this problem.
Note that the input file has been encrypted. For each string occured, let L be the result of the last "?" operation. The string given to you have 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 and total length of the PA Ragraphs 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).
operation, output a line containing the result.
Sample Input
23+01+01?010013+01?010?011

Sample Output
Case #1:2Case #2:10

Source2013 Asia Chengdu Regional Contest
Recommendwe has carefully selected several similar problems for you:  5368 5367 5366 5365  5364  The main idea: +w said to learn a word,? P asked to learn the number of words (first, the word is different, the second, the count is all the number of occurrences in the string, not the number of words that exist) test instructions really began to understand the wrong, such as +10+10? 101 should be 1+10? 101010 is supposed to be 3. Then the text is encrypted, the previous calculated value, and the string behind it has moved so many bits like left, whether it's + or? Of!!! Idea: Start like with violence point, offline do, directly is the template,, has been re, because re-inserted after re-build_ac a back, really violent point, and then Baidu see people are all with two AC automatic machine realized online AC automaton, and then learn a bit, Probably the idea is to set up two AC automata, a large one small, small is equivalent to the cache, first to small storage. There is an upper limit (the upper limit should be the maximum number of nodes of the root, but I try to find a few of the difference is not too much), to reach the upper limit on the large AC automata merged, combined more classic ... Specific look at the code,,, in this way to achieve online query,, in the modification of the online code is, found that the shift function is wrong, and changed the past, suddenly think of the offline code may be because of this re, and then changed after WA,, did not time out,, suddenly excited, and changed the past, Haha, Offline can also do the AC code (online AC automaton) annotation of the kind of writing also line VISAC code
#include <stdio.h> #include <string.h> #include <stdlib.h> #pragma comment (linker, "/stack      : 1024000000,1024000000 ") char Str[5000100],s1[5000100];int head,tail;int node_num;struct Node {node *fail;      Node *next[2];      __int64 Vis,key;          Node () {fail=null;          key=vis=0;      for (int i=0;i<2;i++) next[i]=null;  }}*q[8008000];      Node *root1,*root2;void Insert (char *s,node *root) {int temp,len,i;      Node *p=root;      Len=strlen (s);        for (i=0;i<len;i++) {temp=s[i]-' 0 ';            if (p->next[temp]==null) {node_num++;        P->next[temp]=new node ();}      p=p->next[temp];      } p->vis=1;    } void Build_ac (node *root) {head=tail=0;    Q[tail++]=root;          while (Head!=tail) {node *p=q[head++];          Node *temp=null;             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->ne                    xt[i]->key=p->next[i]->vis;              }} q[tail++]=p->next[i];      }}}} __int64 query (char *str,node *root) {__int64 ans=0;      int Len=strlen (str);      Node *p=root,*temp;        for (int i=0;i<len;i++) {  int x=str[i]-' 0 ';          while (P->next[x]==null&&p!=root) p=p->fail;          p=p->next[x];          if (p==null) {p=root;         } temp=p;              /*while (temp!=root) {ans+=temp->vis;         temp=temp->fail;    } */ans+=temp->key; } return ans; int seach (char *s,node *root) {int Len=strlen (s), I,j,now;node *cur=root;for (i=0;i<len;i++) {now=s[i]-' 0 '; if (cur- >next[now]!=null) {Cur=cur->next[now];} Elsereturn 0;} if (Cur->vis) return 1;return 0;} void del (node *root) {if (root==null) Return;int i;for (i=0;i<2;i++) {if (root->next[i]!=null) del (root->next[i ]);} Free (root);} void Union (node *root1,node *root2)//merge AC Automaton 2 to 1 {head=tail=0;q[tail++]=root1;q[tail++]=root2;int i,j;while (head!= Tail) {node *r1=q[head++];node *r2=q[head++];for (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[tail++]=r1->next[i];q[tail++]=r2->next[i];}}} void Shilf (char *str,__int64 num)//String left shift 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];} str[len]=0;} int main () {int t,c=0;scanf ("%d", &t), while (t--) {int n;root1=new node (); root2=new node (); scanf ("%d", &n); printf ("Case #%d:\n", ++c), int i;__int64 num=0;node_num=0;build_ac (ROOT1), and for (i=0;i<n;i++) {scanf ("%s", str); if ( str[0]== ' + ') {Shilf (str+1,num); if (seach (STR+1,ROOT1) | | Seach (Str+1,root2)) Continue;insert (Str+1,root2), Build_ac (Root2), if (node_num>2500) {Union (Root1,root2);d El ( ROOT2); root2=new node (); Build_ac (ROOT1); Build_ac (root2); node_num=0;}} ELSE{SHILF (Str+1,num),//printf ("%s\n", str+1), __int64 ans=query (str+1,root1) +query (Str+1,root2); num=ans;printf (" %i64d\n ", ans);}} Del (ROOT1);d El (Root2);}}
AC code (offline AC automata on)
#include <stdio.h> #include <string.h>char str[5000010],s1[5000010];int head,tail;      struct Node {node *fail;      Node *next[2];      __int64 CNT;          Node () {fail=null;          cnt=0;      for (int i=0;i<2;i++) next[i]=null;  }}*q[8000800];  Node *root;      void Insert (char *s) {int temp,len,i;      Node *p=root;      Len=strlen (s);          for (i=0;i<len;i++) {temp=s[i]-' 0 ';          if (P->next[temp]==null) p->next[temp]=new node ();      p=p->next[temp];      } p->cnt++;    } void Build_ac () {head=tail=0;      Q[tail++]=root;          while (Head!=tail) {node *p=q[head++];          Node *temp=null;                      for (int i=0;i<2;i++) {if (p->next[i]!=null) {if (p==root)                  p->next[i]->fail=root;               else {temp=p->fail;       while (Temp!=null) {if (temp->next[i]!=null)                              {p->next[i]->fail=temp->next[i];                          Break                      } temp=temp->fail;                      } if (temp==null) {p->next[i]->fail=root;              }} q[tail++]=p->next[i];      }}}} __int64 query (char *str) {__int64 ans=0;      int Len=strlen (str);      Node *p=root,*temp;          for (int i=0;i<len;i++) {int x=str[i]-' 0 ';          while (P->next[x]==null&&p!=root) p=p->fail;          p=p->next[x];          if (p==null) {p=root;          } temp=p;  while (temp!=root)//no &&temp->cnt {ans+=temp->cnt; Temp->cnt=-1;//temp->cnt=0;          temp=temp->fail; }} return ans;    void shift (char *str,__int64 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->next[ Now]!=null) {Cur=cur->next[now];} Elsereturn 0;} if (cur->cnt) return 1;return 0;}    int main () {int t,c=0;    scanf ("%d", &t);        while (t--) {int n;        Root=new node ();        scanf ("%d", &n);p rintf ("Case #%d:\n", ++c);        int i;        __int64 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_ac ();                __int64 ans=query (str+1);                Num=ans;            printf ("%i64d\n", ans); }        }    }}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj topic 4787 GRE Words Revenge (online ac automata, offline also can do)

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.