HDU - 1686 Oulipo (KMP)

2024-06-05 21:32
文章标签 hdu kmp 1686 oulipo

本文主要是介绍HDU - 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.


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的基本应用
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1000010;char a[maxn], b[maxn];
int next[maxn];
int n, m;void getnext() {int m = strlen(a);next[0] = 0, next[1] = 0;for (int i = 1; i < m; i++) {int j = next[i];while (j && a[i] != a[j])j = next[j];next[i+1] = a[i] == a[j] ? j+1 : 0;}
}int kmp() {int ans = 0;int n = strlen(b);int m = strlen(a);int j = 0;for (int i = 0; i < n; i++) {while (j && a[j] != b[i])j = next[j];if (a[j] == b[i])j++;if (j == m)ans++;}return ans;
}int main() {int t;scanf("%d", &t);while (t--) {scanf("%s%s", a, b);getnext();int ans = kmp();printf("%d\n", ans);}return 0;
}


这篇关于HDU - 1686 Oulipo (KMP)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1034230

相关文章

上海邀请赛 A题目 HDU 5236(dp)

先求出没有ctrl+s的时候构造长度为i的期望f[i] 。然后枚举保存的次数,求出最小即可。 #include<cstdio>#include<cstdio>#include<cmath>#include<queue>#include<stack>#include<string>#include<cstring>#include<iostream>#include<map>

hdu 2586 树上点对最近距离 (lca)

,只要知道dis[i][j]=dis[i][root]+dis[j][root]-2*dis[Lca(i,j)][root].   其中root为树的根节点,LCA(i,j)为i,j的最近公共祖先。 所以我们先把所有的询问储存下来,然后离线直接查询。复杂度是o(n+q)的。 VIE #include<cstdio>#include<algorithm>#include<i

HDU_1013

这个题目尤其需要注意的是开始输入的时候的数的大小,开始输入时有可能非常的大超过了长整型的范围,所以不能开始用整形来存放输入的,,就是开始的时候没有注意到这个问题所以开始的时候一直没有AC,后来就是用一个数组接收输入,然后在经过第一步转化之后就可以用一个整数来装了 #include <iostream>#include <stdlib.h>#include <string>usin

最长回文子串(百度笔试题和hdu 3068)

版权所有。所有权利保留。 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/17123559 求一个字符串的最长回文子串。注意子串是连续的,子序列是不连续的。对于最长回文子序列,要用动态规划解,具体请看: http://blog.csdn.net/xiaofei_it/article/details/1

杭电ACM hdu 2110 Crisis of HDU 解题报告(母函数)

Problem Description 话说上回讲到HDU大战东洋小苟,结果自然是中方大胜,这一战也使得海东集团在全球同行业中的地位更加巩固。随着集团的发展,很多创业时期的元老逐步功成身退,先是8600移民海外,然后是linle夫妇退隐山林,逐渐的,最初众多的元老只剩下XHD夫妇和Wiskey三人了。 到了2020年,因为扩张过度加上老鼠数量逐年减少,公司的发展遇到了前所未有的危机,此时集团已经

杭电ACM hdu 2082 找单词 解题报告(母函数)

Problem Description 假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26。那么,对于给定的字母,可以找到多少价值<=50的单词呢?单词的价值就是组成一个单词的所有字母的价值之和,比如,单词ACM的价值是1+3+14=18,单词HDU的价值是8+4+21=33。(组成的单词与排列顺序无关,比如

杭电ACM hdu 2079 选课时间 解题报告(母函数)

Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合。你来帮帮他吧。(xhd认为一样学分的课没区别)   Input 输入数据的第一行是一个数据T,表示有T组数据。 每组数据的第一行是两个整数n(1 <= n <= 40),k(1 <= k <= 8)。 接着有k行,每行有两个整数a(1 <= a <= 8),b

字符串的特征向量与KMP算法

字符串的特征向量就是由字符串各位置上的特征数构成的一个向量。设字符串为P,令Pi为从字符串首字母到第i个位置的前缀,则字符串P的i位置上的特征数就是Pi的首尾非空真子串匹配的最大长度。例如:字符串abcdaabcab的特征向量是(0,0,0,0,1,1,2,3,1,2)。其中第5个位置的特征数是1,因为P5是abcdaa,首尾非空真子串能够匹配的就是a;而第7个位置的特征数是3,因为P7是abcd

[HDU 4855] Goddess (极角排序+三分)

HDU - 4855 借这道题学了下极角排序和三分求凸函数最大值 按所有左右切线,圆心的弧度值排序,从而分割出角度上的若干个区间 射线的角度在这些区间里变动时,每个割线的长度都是凸函数,叠加起来也是凸函数 所以可以对每个区间利用三分法求得最大值,再对这些最大值取 max 即为答案 1) 利用三分法可以求得单峰函数的最大值 2) 利用 atan2(y,x)可以比较方便地求出向量与 x轴正

[HDU 5029] Relief grain (树链剖分+线段树)

HDU - 5029 其实这道题最大的难点不是树链剖分,而是怎么维护某个点被那些颜色染过,染过多少次 如果在线段树维护的话,很难做到,估计得树套树,而且空间会炸 好在这题是离线的,可以使用差分的思想来维护 对一段区间[l,r]染色 c,相当于在这段区间左端点 l打上 c标志,右端点 r+1打上 -c标志 然后扫一遍整个区间 (依照 dfs序扫一遍整棵树),期间不断维护一颗线段树 线段树