HUD-1686(kmp算法)

Oulipo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18075    Accepted Submission(s): 7124

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.

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,试了十几回,总是超时。以为要优化,后来发现是数组没开够。qwq
 1 #include<stdio.h>
 2 #include<string.h>
 3 const int maxn=10006;
 4 int nex[maxn];
 5 char str[maxn*100],ptr[maxn];
 6 
 7 void get_next(int n)
 8 {
 9     int k=-1;
10     nex[0]=-1;
11     for(int i=1;i<n;i++){
12         while(k>-1&&ptr[k+1]!=ptr[i])
13             k=nex[k];
14         if(ptr[k+1]==ptr[i])
15             k++;
16         nex[i]=k;
17     }
18 }
19 
20 int kmp(int n,int m)
21 {
22     get_next(m);
23     int ans=0,k=-1,i=0;
24     for(;i<n;i++){
25         while(k>-1&&str[i]!=ptr[k+1])
26             k=nex[k];
27         if(ptr[k+1]==str[i]){
28             k++;
29             if(k+1==m)
30                 ans++;
31         }
32     }
33     return ans;
34 }
35 
36 int main()
37 {
38     int N;
39     scanf("%d",&N);
40     while(N--){
41         getchar();
42         scanf("%s%s",ptr,str);
43         int n,m;
44         n=strlen(str);
45         m=strlen(ptr);
46         printf("%d\n",kmp(n,m));
47     }
48     
49     return 0;
50 }
View Code

有两种kmp。

 1 #include<stdio.h>
 2 #include<string.h>
 3 const int maxn=10006;
 4 int nex[maxn];
 5 char str[maxn*100],ptr[maxn];
 6 
 7 void get_next(int n)
 8 {
 9     int k=-1;
10     nex[0]=-1;
11     for(int i=0;i<n;){
12         if(k==-1||ptr[i]==ptr[k]){
13             k++,i++;
14             nex[i]=k;
15         }
16         else{
17             k=nex[k];
18         }
19     }
20 }
21 
22 int kmp(int n,int m)
23 {
24     get_next(m);
25     int ans=0,k=0,i=0;
26     while(i<n){
27         if(k==-1||ptr[k]==str[i]){
28             ++k,++i;
29             if(k==m){
30                 ++ans;
31             }
32         }
33         else
34             k=nex[k];
35     }
36     return ans;
37 }
38 
39 int main()
40 {
41     int N;
42     scanf("%d",&N);
43     while(N--){
44         getchar();
45         scanf("%s%s",ptr,str);
46         int n,m;
47         n=strlen(str);
48         m=strlen(ptr);
49         printf("%d\n",kmp(n,m));
50     }
51     
52     return 0;
53 }
View Code

 

posted @ 2018-03-22 18:13  flyer_duck  阅读(221)  评论(0编辑  收藏  举报