标签:
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:
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<vector> 7 #include<queue> 8 using namespace std; 9 const int maxn=1000010; 10 int tot,ANS; 11 char T[maxn],P[maxn]; 12 int lenT,lenP; 13 int next[maxn]; 14 inline void get_next(){ 15 int j=1; 16 for(int i=2;i<=lenP;i++){ 17 if(P[j]==P[i]) next[i]=j,j++; 18 else{ 19 if(P[i]==P[1]) next[i]=1; 20 j=next[i]+1; 21 } 22 } 23 } 24 inline void find_pos(){ 25 int j=1; 26 for(int i=1;i<=lenT;){ 27 if(P[j]==T[i]){ 28 if(j==lenP){ 29 ANS++; 30 j=next[j]+1; 31 i++; 32 } 33 else i++,j++; 34 } 35 else{ 36 if(j==1) i++; 37 else j=next[j-1]+1; 38 } 39 } 40 } 41 int main(){ 42 scanf("%d",&tot); 43 for(int i=1;i<=tot;i++){ 44 ANS=0; 45 memset(next,0,sizeof(next)); 46 memset(P,0,sizeof(P)); 47 memset(T,0,sizeof(T)); 48 scanf("%s%s",P+1,T+1); 49 lenT=strlen(T+1); lenP=strlen(P+1); 50 get_next(); find_pos(); 51 cout<<ANS<<endl; 52 } 53 return 0; 54 }
标签:
原文地址:http://www.cnblogs.com/CXCXCXC/p/4936469.html