Oulipo
64-bit integer IO format: %lld Java class name: Main
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
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 using namespace std; 5 const int MaxN = 1000010; 6 char word[MaxN/10], txt[MaxN]; 7 int next[MaxN/10]; 8 void KMP_next(char b[], int pre[]) { 9 int n = strlen(b), k; 10 pre[0] = -1; 11 k = -1; 12 for(int i = 1; i < n; i++) { 13 while(k > -1 && b[k+1] != b[i]) k = pre[k]; 14 if(b[k+1] == b[i]) k++; 15 pre[i] = k; 16 } 17 } 18 19 int main() { 20 int n; 21 scanf("%d%*",&n); 22 while(n--) { 23 gets(word); 24 gets(txt); 25 KMP_next(word, next); 26 int cnt = 0, len = strlen(word); 27 for(int i = 0, j = -1; txt[i]; ++i) { 28 while(j > -1 && word[j+1] != txt[i]) j = next[j]; 29 if(word[j+1] == txt[i]) j++; 30 if(j == len-1) { 31 cnt++; 32 j = next[j]; 33 } 34 } 35 printf("%d ", cnt); 36 } 37 return 0; 38 }
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int maxn = 1000005; 6 int fail[maxn]; 7 void getNext(const char *pStr, int *nextArr) { 8 int i = 0, j = -1, pLen = strlen(pStr); 9 nextArr[i] = j; 10 while (i < pLen) { 11 if (pStr[++i] != pStr[++j]) { 12 nextArr[i] = j; 13 while (j != -1 && pStr[i] != pStr[j]) j = nextArr[j]; 14 } else nextArr[i] = nextArr[j]; 15 } 16 } 17 char word[maxn],text[maxn]; 18 int main(){ 19 int n,ret; 20 scanf("%d",&n); 21 while(n--){ 22 scanf("%s %s",word,text); 23 getNext(word,fail); 24 for(int i = ret = 0,j = 0; text[i]; ++i){ 25 while(j != -1 && word[j] != text[i]) j = fail[j]; 26 if(!word[++j]) ret++; 27 } 28 printf("%d ",ret); 29 } 30 return 0; 31 }
整理以后的,可以选择开启所谓的优化
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 const int maxn = 1000005; 6 int fail[maxn]; 7 char word[maxn],text[maxn]; 8 void getFail() { 9 fail[0] = -1; 10 fail[1] = 0; 11 for(int i = 0,j = -1; word[i]; ++i) { 12 while(j != -1 && word[i] != word[j]) j = fail[j]; 13 fail[i+1] = ++j; 14 if(word[i+1] == word[j]) fail[i+1] = fail[j];//使用此句加优化,注释掉不加优化,都是正确的 15 } 16 } 17 int main() { 18 int n,ret; 19 scanf("%d",&n); 20 while(n--) { 21 scanf("%s%s",word,text); 22 getFail(); 23 for(int i = ret = 0, j = 0; text[i] ; ++i) { 24 while(j > -1 && word[j] != text[i]) j = fail[j]; 25 if(!word[++j]) {ret++;j = fail[j];} 26 } 27 printf("%d ",ret); 28 } 29 return 0; 30 }