Oulipo
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6227 Accepted Submission (s): 2513
Problem DescriptionThe French author Georges perec (1936–1982) once wrote a book, La disparition, without the letter ' E '. He was a member of the Oulipo group. A quote from the book:
Tout avait Pair normal, mais Tout s ' affirmait faux. Tout avait Fair normal, d ' Abord, puis surgissait l ' inhumain, l ' affolant. Il aurait voulu savoir oùs ' articulait l ' association qui l ' unissait au roman:stir son tapis, Assaillantàtout instant s On imagination, l ' intuition d ' un tabou, la vision d ' un mal obscur, d ' un quoi vacant, d ' UN non-dit:la vision, l ' avision D ' Un oubli commandant tout, oùs ' abolissait la raison:tout avait l ' air normal mais ...
Perec would probably has scored high (or rather, low) in the following contest. People is asked to write a perhaps even meaningful text on some subject with as few occurrences of a given "word" as poss Ible. Our tasks are to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the Competit Ors. These competitors often write very long texts with nonsense meaning; A sequence of 500,000 consecutive ' T ' is not unusual. And they never use spaces.
So we want to quickly find out what often a word, i.e., a given string, occurs in a text. More Formally:given the alphabet {' A ', ' B ', ' C ', ..., ' Z '} and both finite strings over that alphabet, a word W and a text T , count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. occurrences may overlap.
Inputthe first line of the input file contains a single number:the number of test cases to follow. Each test case has the following format:
One line with the word W, a string over {' A ', ' B ', ' C ', ..., ' Z '}, with 1≤| w| ≤10,000 (Here | w| Denotes the length of the string W).
One line with the text T, a string over {' A ', ' B ', ' C ', ..., ' Z '}, with | w| ≤| T| ≤1,000,000.
Outputfor every test case in the input file, the output should contain a single number, on a single line:the number of OC Currences of the word W in the text T.
Sample Input3bapcbapcazaazazazaverdiaverdxivyerdian
Sample Output130
Source East University undergraduate Program Design Invitational-warm-up AC automata Practice: Code:
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <iostream>5#include <queue>6 7 using namespacestd;8 Const intmaxn= -;9 Ten structnode{ One ANode *CHILD[MAXN]; -node * FAIL;//failed Pointer - inttail; the - }; - voidInit (node *tmp) { - for(intI=0; i<maxn;i++) +tmp->child[i]=NULL; -Tmp->fail=NULL; +Tmp->tail=0; A } at - - //Build a dictionary tree - - voidBuildtree (Const CharSS [], node *root) { -Node *cur; in for(intI=0; Ss[i]; i++){ - intNo =ss[i]-'A'; to if(root->child[no]==NULL) { +Cur =Newnode; - init (cur); theroot->child[no]=cur; * } $root = root->Child[no];Panax Notoginseng } -root->tail++; the } + A //construct failed pointer the voidBuildfail (Node *root) { + -Queue<node *>tmp; $ Tmp.push (root); $Node *cur,*Po; - while(!Tmp.empty ()) { -Cur =Tmp.front (); theTmp.pop ();//out Queue - for(intI=0; I<MAXN; i++){Wuyi if(Cur->child[i]! =NULL) { the if(cur==root) { -cur->child[i]->fail=root;//point to Root Wu}Else{ -PO =cur; About while(po->fail) { $ if(po->fail->Child[i]) { -Cur->child[i]->fail=po->fail->Child[i]; - Break; - } APO = po->fail; + } the if(po->fail==NULL) -Cur->child[i]->fail=Root; $ } theTmp.push (cur->child[i]); the } the } the } - } in the //Enquiry the intQuery (CharSS [], node *root) { About theNode *cur, *tmp; theCur =Root; the intpos=-1, res=0; + - for(intI=0; Ss[i]; i++){ the Bayipos= ss[i]-'A'; the while(cur->child[pos]==null&&cur!=root) theCur = cur->fail; -Cur = cur->Child[pos]; - if(cur==null) cur=Root; theTMP =cur; the while(tmp!=root&&tmp->tail>0){ theRes+=tmp->tail; theTMP = tmp->fail; - } the } the the returnRes;94 the } the Charsa[10010],sb[1000010]; the intMain () {98 intte; AboutNode *Root; -scanf"%d",&te);101 while(te--){102Root =Newnode;103 init (root);104scanf"%s%s", SA,SB); the Buildtree (sa,root);106 Buildfail (root);107 intAns =Query (sb,root);108printf"%d\n", ans);109 } the return 0;111}
HDU----1686 Oulipo (AC automaton)