http://poj.org/problem?id=3461
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 42698 | Accepted: 17154 |
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 son 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 have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.
So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two 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 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.
Output
For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.
Sample Input
3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN
Sample Output
1 3 0
Source
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 6 using namespace std; 7 8 #define p 147 9 #define ull unsigned long long 10 char s1[10005],s2[1000005]; 11 ull n,ss1,ss2,P; 12 ull lens,hs[1000005],ans; 13 14 inline void Get_hash() 15 { 16 lens=0; 17 for(ull i=1;i<=ss1;i++) 18 lens=lens*p+s1[i]-'A'; 19 } 20 inline void Get_hs() 21 { 22 for(ull i=1;i<=ss2;i++) 23 hs[i]=hs[i-1]*p+s2[i]-'A'; 24 } 25 inline ull Q_pow(ull a,ull b) 26 { 27 ull ret=1,base=a; 28 for(;b;b>>=1,base*=base) 29 if(b&1) ret*=base; 30 return ret; 31 } 32 inline ull Check(ull x,ull y) 33 { 34 return hs[y]-hs[x-1]*P; 35 } 36 37 int main() 38 { 39 for(cin>>n;n--;ans=0) 40 { 41 scanf("%s%s",s1+1,s2+1); 42 ss1=strlen(s1+1),ss2=strlen(s2+1); 43 Get_hash(); Get_hs(); P=Q_pow(p,ss1); 44 for(ull i=ss1;i<=ss2;i++) 45 if(lens==Check(i-ss1+1,i)) ans++; 46 cout<<ans<<endl; 47 } 48 return 0; 49 }
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 6 using namespace std; 7 8 int n,ss1,ss2; 9 char s1[10005],s2[1000005]; 10 int lens,next[1000005],ans; 11 12 inline void Get_next() 13 { 14 int j=0;next[1]=0; 15 for(int i=2;i<=ss1;i++) 16 { 17 for(;j>0&&s1[i]!=s1[j+1];) j=next[j]; 18 if(s1[i]==s1[j+1]) j++; 19 next[i]=j; 20 } 21 } 22 23 inline void kmp() 24 { 25 int j=0; 26 for(int i=1;i<=ss2;i++) 27 { 28 for(;j>0&&s2[i]!=s1[j+1];) j=next[j]; 29 if(s1[j+1]==s2[i]) j++; 30 if(j==ss1) ans++,j=next[j]; 31 } 32 } 33 34 int main() 35 { 36 for(cin>>n;n--;ans=0) 37 { 38 scanf("%s%s",s1+1,s2+1); 39 ss1=strlen(s1+1),ss2=strlen(s2+1); 40 Get_next(); kmp(); 41 printf("%d ",ans); 42 } 43 return 0; 44 }