Oulipo
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 30723 | Accepted: 12349 |
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
题意:给若干组数据,每组两个字符串,问第一个字符串在第二个字符串中出现的次数
思路:KMP算法来判断长串中的子串是否和短串相等,
注意:如果发现一组匹配,让 ANS++,j=next[j]+1,i++,因为next[i]=x的意思是以1为起始的前缀和以i为结束的后缀的长度为x,这样可以看出此时P[next[i]]==P[1],这样就避免了让 j=1,i=i-lenP+2,避免了时间的浪费。由KMP的next构造可知,P[10]={\000AAAA}的next={0,0,1,2,3,},P的前缀和后缀是可以重叠的,而且会让每个的next[i]的值尽量大,所以可以保证不会漏掉某一种情况
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cmath> 5 #include<algorithm> 6 #include<cstring> 7 using namespace std; 8 char T[1000010],P[10010]; 9 int N,next[10010],lenP,lenT,ans; 10 int main(){ 11 scanf("%d",&N); 12 while(N--){ 13 ans=0; 14 scanf("%s%s",P+1,T+1); 15 lenP=strlen(P+1); lenT=strlen(T+1); 16 //get next[] 17 for(int j=0,i=2;i<=lenP;i++){ 18 while(P[j+1]!=P[i]&&j) j=next[j]; 19 if(P[j+1]==P[i]) j++; 20 next[i]=j; 21 } 22 23 for(int i=1,j=1;i<=lenT;){ 24 if(P[j]==T[i]){ 25 if(j==lenP){ 26 ans++; 27 j=next[j]+1; 28 i++; 29 } 30 else i++,j++; 31 } 32 else{ 33 if(j==1) i++; 34 else j=next[j-1]+1; 35 } 36 } 37 printf("%d\n",ans); 38 } 39 return 0; 40 }
还可以用hash
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cmath> 5 #include<algorithm> 6 #include<vector> 7 #include<queue> 8 using namespace std ; 9 typedef unsigned long long uLL ; 10 const int N=1000005 ; 11 const uLL Base=1234567 ; 12 uLL base[N]={1},hs,hA[N] ; 13 inline uLL ask(int l,int r) 14 {return hA[r]-hA[l-1]*base[r-l+1] ;} 15 16 char A[N],s[N] ; 17 int T,ans,n,m ; 18 int main() 19 { 20 for(int i=1;i<N;i++) 21 base[i]=base[i-1]*Base ; 22 scanf("%d",&T); 23 while( T-- ) 24 { 25 ans=0 ; hs=0 ; 26 memset(hA,0,sizeof(hA)) ; 27 28 scanf("%s%s",s+1,A+1); 29 m=strlen(A+1) ; n=strlen(s+1) ; 30 31 for(int i=1;i<=m;i++) 32 { 33 hA[i]=hA[i-1]*Base+(A[i]-'A'+1) ; 34 if(i<=n) hs=hs*Base+(s[i]-'A'+1) ; 35 } 36 for(int i=m-n+1; i; i--) 37 if(ask(i,i+n-1)==hs) ans++ ; 38 39 printf("%d\n",ans) ; 40 } 41 return 0; 42 }