Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40168 Accepted: 16135 Description The French author Georges Perec (1936–1982) once w ...
Oulipo
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 40168 | Accepted: 16135 |
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
BAPC 2006 Qualification 莫名其妙TLE1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 char a[10001]; 6 char b[1000001]; 7 int p[10001]; 8 int la,lb; 9 int j; 10 int ans; 11 inline void makep() 12 { 13 14 j=0; 15 for(int i=1;i<la;i++) 16 { 17 while(j>0&&a[i]!=a[j]) 18 j=p[j]; 19 if(a[i]==a[j]) 20 j++; 21 p[i]=j; 22 } 23 return ; 24 } 25 inline void KMP() 26 { 27 j=0; 28 ans=0; 29 for(int i=0;i<lb;i++) 30 { 31 while(b[i]!=a[j]&&j>0) 32 j=p[j-1]; 33 if(b[i]==a[j]) 34 j++; 35 if(j==la) 36 { 37 ans++; 38 j=p[j-1]; 39 } 40 41 } 42 printf("%d\n",ans); 43 return ; 44 } 45 46 int main() 47 { 48 int n; 49 scanf("%d",&n); 50 51 for(int i=1;i<=n;i++) 52 { 53 memset(p,0,sizeof(p)); 54 scanf("%s%s",a,b); 55 la=strlen(a);lb=strlen(b); 56 makep(); 57 KMP(); 58 } 59 60 61 return 0; 62 }TLE
AC
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 using namespace std; 6 7 int n,la,lb; 8 int next[100010]; 9 char a[1000010],b[100010]; 10 11 void getnext() 12 { 13 int j=-1; 14 next[0]=-1; 15 for(int i=1;i<lb;i++) 16 { 17 if(j!=-1 && b[j+1]!=b[i]) j=next[j]; 18 if(b[j+1]==b[i]) j++; 19 next[i]=j; 20 } 21 } 22 23 int kmp() 24 { 25 int ans=0; 26 int j=-1; 27 for(int i=0;i<la;i++) 28 { 29 if(j!=-1 && a[i]!=b[j+1]) j=next[j]; 30 if(b[j+1]==a[i]) j++; 31 if(j==lb-1) 32 { 33 ans++; 34 j=next[j]; 35 } 36 } 37 return ans; 38 } 39 40 int main() 41 { 42 scanf("%d",&n); 43 for(int u=1;u<=n;u++) 44 { 45 scanf("%s %s",b,a); 46 la=strlen(a); 47 lb=strlen(b); 48 getnext(); 49 printf("%d\n",kmp()); 50 } 51 }AC