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

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 }


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

时间: 2024-08-09 06:36:57

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

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher :G - Power Strings POJ - 2406(kmp简单循环节)

[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher G - Power Strings POJ - 2406 题目: Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; ManacherK - Count the string HDU - 3336(前缀数量问题)

K - Count the string HDU - 3336 题目链接:https://vjudge.net/contest/70325#problem/K 题目: It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of t

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher H - Seek the Name, Seek the Fame POJ - 2752(kmp的next数组应用)

H - Seek the Name, Seek the Fame POJ - 2752 题目链接:https://vjudge.net/contest/70325#problem/H 题目: The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. Th

[kuangbin带你飞]专题十 匹配问题 一般图匹配

过去做的都是二分图匹配 即 同一个集合里的点 互相不联通 但是如果延伸到一般图上去 求一个一般图的最大匹配 就要用带花树来解决 带花树模板 用来处理一个无向图上的最大匹配 看了一会还是不懂  抄了一遍kuangbin的模板熟悉了一下 还有一个一般图最大权匹配 保存下来了VFK菊苣的模板题代码当作板子 http://uoj.ac/submission/16359 但愿以后的比赛永远也遇不到 .. 遇到了也能抄对 .. 抄错了也能过 .. R ural1099 kuangbin模板 #include

[kuangbin带你飞]专题十 匹配问题 二分图多重匹配

二分图的多重匹配问题不同于普通的最大匹配中的"每个点只能有最多一条边" 而是"每个点连接的边数不超过自己的限定数量" 最大匹配所解决的问题一般是"每个人都有一群想加入的团体 并且一个团体只能收一个人 问有多少人可以加入一个自己喜欢的团体" 而多重匹配是 "每个人都有一群想加入的团体 每个团体可以收给定的人数 问有多少人可以加入一个自己喜欢的团体" 解决这个问题 目前看貌似有三个办法 1 拆点 一个团体可以招x个人 就把它拆成x

【算法系列学习】DP和滚动数组 [kuangbin带你飞]专题十二 基础DP1 A - Max Sum Plus Plus

A - Max Sum Plus Plus 1 https://vjudge.net/contest/68966#problem/A 2 3 http://www.cnblogs.com/kuangbin/archive/2011/08/04/2127085.html 4 5 /* 6 状态dp[i][j]有前j个数,组成i组的和的最大值.决策: 7 第j个数,是在第包含在第i组里面,还是自己独立成组. 8 方程 dp[i][j]=Max(dp[i][j-1]+a[j] , max( dp[i-

【算法系列学习】状压dp [kuangbin带你飞]专题十二 基础DP1 D - Doing Homework

https://vjudge.net/contest/68966#problem/D http://blog.csdn.net/u010489389/article/details/19218795 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath>

【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 G - 免费馅饼

https://vjudge.net/contest/68966#problem/G 正解一: http://www.clanfei.com/2012/04/646.html 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath> 7 #define IN

【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 E - Super Jumping! Jumping! Jumping!

https://vjudge.net/contest/68966#problem/E http://blog.csdn.net/to_be_better/article/details/50563344 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath>