码迷,mamicode.com
首页 > 其他好文 > 详细

[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher B - Oulipo HDU - 1686(kmp)

时间:2019-08-13 22:56:09      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:clu   -o   iat   panel   print   some   create   pair   int   

B - Oulipo HDU - 1686

题目链接:https://vjudge.net/contest/70325#problem/B

题目:

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.


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
题意:给你两个字符串a,b,求出a在b中出现了多少次,用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 }

 


[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher B - Oulipo HDU - 1686(kmp)

标签:clu   -o   iat   panel   print   some   create   pair   int   

原文地址:https://www.cnblogs.com/Vampire6/p/11348947.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!