poj 3461 Oulipo
Time Limit:1000MS | Memory Limit:65536K | |
Total Submissions:45636 | Accepted:18224 |
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 wordWand a textT, count the number of occurrences ofWinT. All the consecutive characters of W must exactly match consecutive characters ofT. 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 wordW, a string over {'A','B','C', …,'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the stringW).
- One line with the textT, 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 wordWin the textT.
Sample Input
3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN
Sample Output
1 3 0
题意:
给你两个字符串,求第二个字符串在第一个字符串中
出现的次数。
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #include<string> 7 using namespace std; 8 const int N=1e6+90; 9 char s1[N],s2[N]; 10 int T,len1,ans,len2,ne[N],i,j; 11 void kmp() 12 { 13 for(int i=1;i<=len2;++i) ne[i]=0; 14 for(int i=2;i<=len2;++i) 15 { 16 j=ne[i-1]; 17 while(j && s2[i]!=s2[j+1]) j=ne[j]; 18 if(s2[i]==s2[j+1]) ne[i]=j+1; 19 } 20 j=0; 21 for(int i=1;i<=len1;++i) 22 { 23 while(j && s1[i]!=s2[j+1]) j=ne[j]; 24 if(s1[i]==s2[j+1]) j=j+1; 25 if(j==len2) ans++; 26 } 27 } 28 int main() 29 { 30 scanf("%d",&T); 31 while(T--) 32 { 33 scanf("%s%s",s2+1,s1+1); 34 len1=strlen(s1+1);len2=strlen(s2+1); 35 ans=0; 36 kmp(); 37 printf("%d\n",ans); 38 } 39 return 0; 40 }/* 41 Sample Input 42 43 3 44 BAPC 45 BAPC 46 AZA 47 AZAZAZA 48 VERDI 49 AVERDXIVYERDIAN 50 Sample Output 51 52 1 53 3 54 0 55 */