[Kuangbin take you to fly] KMP & topic sixteen extended KMP & Manacher B - Oulipo HDU - 1686 (kmp)

B - Oulipo HDU - 1686

Topic links: https://vjudge.net/contest/70325#problem/B

topic:

The French author Georges Perec (1936-1982) wrote a book ounce, disappearance, without the letter 'e'. He Was a member of the Oulipo group. A quote from the book:

Everything was normal pair, but asserted itself false. All had normal Fair, first, then arose the inhuman, the maddening. He wanted to know where was based association that united the novel stir his carpet assailant at any moment his imagination, intuition of a taboo, the vision of a dark evil of what vacant , an unsaid: vision, a commander avision forgetting everything, where abolished the reason everything looked normal but ...

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.


InputThe 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.
OutputFor 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 
meaning of the questions: to give you two strings a, b, there has been obtained a number of times in (b), you can use kmp

 1 //
 2 // Created by hanyu on 2019/8/13.
 3 //
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <queue>
 9 #include <set>
10 #include<math.h>
11 #include<map>
12 using namespace std;
13 typedef long long ll;
14 const int maxn=1e6+10;
15 char str[maxn],str1[maxn];
16 int nextt[maxn];
17 void getnext()
18 {
19     memset(nextt,0,sizeof(nextt));
20     int i=0,j=-1;
21     nextt[0]=-1;
22     int m=strlen(str1);
23     while(i<m)
24     {
25         if(j==-1||str1[i]==str1[j])
26         {
27            ++i,++j;
28             if(str1[i]!=str1[j])
29                 nextt[i]=j;
30             else
31                 nextt[i]=nextt[j];
32         } else
33             j=nextt[j];
34     }
35 }
36 int kmp()
37 {
38     int res=0;
39     int n=strlen(str);
40     int m=strlen(str1);
41     int i=0,j=0;
42     while(i<n){
43         if(j==-1||str[i]==str1[j])
44             i++,j++;
45         else
46             j=nextt[j];
47         if(j==m)
48         {
49             j=nextt[j];
50             res++;
51         }
52     }
53     return res;
54 }
55 int main()
56 {
57     int T;
58     scanf("%d",&T);
59     getchar();
60     while(T--)
61     {
62         gets(str1);
63         gets(str);
64         getnext();
65         printf("%d\n",kmp());
66     }
67     return 0;
68 }

 

 

Guess you like

Origin www.cnblogs.com/Vampire6/p/11348947.html