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 INPUT3BAPC BAPC Azaazazazaverdiaverdxivyerdian sample Output130 Source East University Program Design Invitational-warm-up Recommendlcy | We have carefully selected several similar problems for you:1358 3336 3746 2203 3374
Code:
1#include <vector>2#include <map>3#include <Set>4#include <algorithm>5#include <iostream>6#include <cstdio>7#include <cmath>8#include <cstdlib>9#include <string>Ten#include <cstring> One#include <queue> A using namespacestd; - #defineINF 0x3f3f3f3f - the Charp[10010],s[1000010]; - intnex[10010]; - - void Get(Char*p) + { - intplen=strlen (p); +nex[0]=-1; A intk=-1, j=0; at while(J <Plen) { - if(k==-1|| P[J] = =P[k]) { -++J; -++K; - if(P[j]! =P[k]) -nex[j]=K; in Else -nex[j]=Nex[k]; to } + Else{ -k=Nex[k]; the } * } $ }Panax Notoginseng - intKmpChar*s,Char*p) the { + intI=0, j=0, ans=0; A intslen=strlen (s); the intplen=strlen (p); + while(I < Slen && j<Plen) { - if(j==-1|| s[i]==P[j]) { $++i; $++J; - } - Else{ thej=Nex[j]; - }Wuyi if(j = = Plen) {//It is important to note that this is to return to the position that Next[j] should return to when the match is complete . theJ=NEX[J];//Example: a= "Aza" b= "Azazaza" after the first end, Next[j] should refer to the position of ' Z ' in a, and then continue to match -ans++; Wu } - } About $ returnans; - } - - intMain () A { + intT; thescanf"%d",&t); - while(t--){ $scanf"%s", p); thescanf"%s", s); the Get(p); theprintf"%d\n", KMP (s,p)); the } -}
Hdu Oulipo (KMP)