Oulipo
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:23667 |
|
Accepted:9492 |
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 locale 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 wowould 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 occurrencesWInT. All the consecutive characters of W must exactly match consecutive charactersT. 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 shoshould contain a single number, on a single line: the number of occurrences of the wordWIn the textT.
Sample Input
3BAPCBAPCAZAAZAZAZAVERDIAVERDXIVYERDIAN
Sample output
130
Source
Bapc 2006 Qualification
Problem: count the number of times that the W string appears in the T string. Based on the data size, the KMP algorithm can be AC.
Code:
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<ctype.h> 5 #include<stdlib.h> 6 #include<stdbool.h> 7 8 #define rep(i,a,b) for(i=(a);i<=(b);i++) 9 #define clr(x,y) memset(x,y,sizeof(x))10 #define sqr(x) (x*x)11 #define LL long long12 13 int i,j,n,num,14 f[1000003];15 16 char p[10003],t[1000003];17 18 int init()19 {20 clr(p,‘\0‘);21 clr(t,‘\0‘);22 clr(f,0);23 num=0;24 25 scanf("%s",p);26 scanf("%s",t);27 28 return 0;29 }30 31 void kmp(char *t, char *p, int *f)32 {33 int n,m,j;34 n = strlen(t); m = strlen(p);35 getFail(p,f);36 37 j = 0;38 39 for(i = 0;i < n;i++) {40 while(j && p[j] != t[i]) j=f[j];41 if(p[j] == t[i]) j++;42 if(j == m) num++;43 }44 45 46 }47 48 void getFail(char p[],int f[])49 {50 int m,j,i;51 m = strlen(p);52 f[0] = 0;f[1] = 0;53 54 for(i = 1;i < m;i++) {55 j = f[i];56 while(j && p[i] != p[j]) j = f[j];57 f[i+1]=p[i]==p[j] ? j+1 : 0;58 }59 }60 61 int main()62 {63 int T;64 scanf("%d",&T);65 66 while(T--) {67 init();68 getFail(p,f);69 kmp(t,p,f);70 printf("%d\n",num);71 }72 return 0;73 }