首页 > 代码库 > hdoj 1686 Oulipo(kmp)
hdoj 1686 Oulipo(kmp)
Problem 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.
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.
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 <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 const int maxn=1e6+10; 6 int Next[maxn]; 7 char str[maxn],mo[maxn]; 8 void getNext(){ 9 int i=0,j=-1,len=strlen(mo); 10 while(i<len) 11 { if (j==-1||mo[i]==mo[j]) 12 Next[++i]=++j; 13 else j=Next[j]; 14 } 15 } 16 int kmp(){ 17 int i=0,j=0,l1=strlen(str),l2=strlen(mo); 18 int ans=0; 19 while(i<l1) 20 { 21 if(j==-1||mo[j]==str[i]) 22 i++,j++; 23 else j=Next[j]; 24 if(j==l2) ans++; 25 } 26 return ans; 27 } 28 int main() 29 { int t; 30 scanf("%d",&t); 31 while(t--){ 32 scanf("%s%s",mo,str); 33 Next[0]=-1; 34 getNext(); 35 //for(int i=0;i<strlen(mo);i++) 36 //cout<<Next[i]<<" "; cout<<endl; 查看next数组 37 printf("%d\n",kmp()); 38 } 39 return 0; 40 } 41 /* 42 后人修改过后的KMP算法 43 针对aaaaabcde 模式串:aaaaax 44 int j=0,i=1; 45 next[1]=0; 46 while(i<t[0]) // t[0]存放模式串大小 47 { 48 if(j==0||T[i]==T[j]) 49 { 50 i++; 51 j++; 52 if(T[i]!=T[j]) 53 next[i]=j; 54 else next[i]=next[j]; 55 } 56 else j=next[j]; 57 } 58 */
hdoj 1686 Oulipo(kmp)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。