B-oulipo
Time limit:1000ms Memory limit:32768kb 64bit IO format:%i64d &%i64u
Submit Status
Description
The 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.
Input
The first line of the input file contains a, 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.
Output
For every test case in the input file, the output should contain a single number, and on a single line:the number of Occurren Ces of the word W in the text T.
Sample Input
3
Bapc
Bapc
AZA
Azazaza
VERDI
Averdxivyerdian
Sample Output
1
3
0
#include <cstdio> #include <algorithm> #include <cmath> #include <string> #include <cstring >//#include <bits/stdc++.h>using namespace Std;template<class t>inline T Read (t&x) {char C; while ((C=getchar ()) <=32) if (c==eof) return 0; BOOL Ok=false; if (c== '-') Ok=true,c=getchar (); for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 '; if (OK) x=-x; return 1;} Template<class t> inline T read_ (t&x,t&y) {return read (x) &&read (y);} Template<class t> inline T read__ (t&x,t&y,t&z) {return read (x) &&read (y) &&read (z);} Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x; if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');} Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} -------ZCC IO template------const int MAXN=1000001;CONST double inf=999999999; #define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#defineM ((l+r) >>1) #define for (i,t,n) for (int i= (t);i< (n); i++) typedef long Long Ll;typedef double db;typedef pair<i nt,int> P; #define BUG printf ("---\ n"), #define MOD 1000000007int Next[maxn];char a[maxn],b[maxn];void getnext (char*s ) {int i=0,j=-1; Next[0]=-1; int Len=strlen (s); while (I<len) {if (j==-1| | S[I]==S[J]) next[++i]=++j; else J=next[j]; }}int KMP (char*p,char*s) {int i=0,j=0,ans=0; int N=strlen (p); int M=strlen (s); while (i<n&&j<m) {if (j==-1| | P[I]==S[J]) i++,j++; else J=next[j]; if (j==m) {ans++; j=next[j];//update next match location}} return ans; int main () {//#ifndef Online_judge//freopen ("In.txt", "R", stdin); Freopen ("Zccccc.txt", "w", stdout); #endif//Online_judge int n,m,i,j,t,k; int T; Read (T); while (t--) {scanf ("%s%s", A, b); GetNext (a); Writeln (KMP (b,a)); } return 0;}
B-oulipo POJ 3461 (KMP find the number of substrings, substrings can overlap)