Oulipo[字符串哈希]
题目
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
思路
题目的意思就是给定一个子字符串和一个长字符串,要求求出在长字符串中寻找子字符串出现的次数。
通过将字符串转化为一个数字既可方便的判断出两个字符串是否相等,这就是哈希的方法。
代码
#include <cstdio>
#include <algorithm>
#include <cstring>using namespace std;typedef unsigned long long ull;const int N = 1e6+10;
char s1[int(1e4+10)], s2[N];
ull power[N]; //预处理
ull H[N]; //长字符串的哈希数组
ull s, b = 27, h = 1<<31; int main(void)
{power[0] = 1;for(int i = 1; i < N; ++i) {power[i] = power[i-1]*b;}int T, n, m, ans;scanf("%d", &T);while(T--) {scanf("%s%s", s1+1, s2+1);n = strlen(s1+1); m = strlen(s2+1);H[0] = 0;for(int i = 1; i <= m; ++i) { //处理长字符串的哈希值H[i] = (H[i-1]*b + (ull)(s2[i]-'A'))%h;}s = 0;for(int i = 1; i <= n; ++i) { //算出子字符串的哈希值s = (s*b+(ull)(s1[i]-'A'))%h;}ans = 0;for(int i = 0; i <= m-n; ++i) { //依次对哈希值进行比较if(s == H[i+n]-H[i]*power[n]) // H数组运用了前缀和的思想,可以在常数时间内算出字符串的哈希值++ans;}printf("%d\n", ans);}return 0;
}
Oulipo[字符串哈希]相关推荐
- 1455:【例题1】Oulipo——字符串哈希
[题目描述] 给出两个字符串s1,s2((只有大写字母),求s1在s2中出现多少次. 例如:s1="ABA",s2="ABAABA",答案为2. [输入] 输入 ...
- ELFhash - 优秀的字符串哈希算法
原 ELFhash - 优秀的字符串哈希算法 分类:算法杂论算法精讲数据结构 (1424) (2) 1.字符串哈希: 我们先从字符串哈希说起 在很多的情况下,我们有可能会获得大量的字符串,每个字符串 ...
- 【CodeForces】961 F. k-substrings 字符串哈希+二分
[题目]F. k-substrings [题意]给定长度为n的串S,对于S的每个k-子串$s_ks_{k+1}...s_{n-k+1},k\in[1,\left \lceil \frac{n}{2} ...
- 138. 兔子与兔子【字符串哈希】
很基础的字符串哈希 #include<bits/stdc++.h> using namespace std; typedef unsigned long long int ull; con ...
- Singing Superstar 字符串哈希-map操作
题意 : 给一长度 < 1e5 的字符串s,q < 1e5次询问,每次问一个长 < 30 的串 t 在s中出现的次数,且t不可重叠. 例 : "abababa"中 ...
- 中石油训练赛 - DNA(字符串哈希)
题目链接:点击查看 题目大意:给出一串只由A,C,G,T组成的字符串,再给出一个数字k,问每个长度为k的连续子串,出现的次数最多是多少次 题目分析:O(n)哈希一下,O(n)更新一下用无序map维护的 ...
- HDU - 3613 Best Reward(字符串哈希)
题目链接:点击查看 题目大意:给出一个字符串,每个字母都有一个贡献值,现在要将这个字符串拆成两个子串,如果子串是回文串的话,贡献就是其中每个字母的贡献和,现在问贡献最大为多少 题目分析:很简单的一道回 ...
- 怎么把字符串变成数组_字符串哈希:从零开始的十分钟包会教程
大家吼啊!这是我下定决心写专栏以来的第二篇文章,请大家多多资瓷!!同样我们先以上次的话起头吧! 恭喜你找到了这篇博客!虽然这个标题看起来非常像是nc营销号的标题但是!请相信我一次这是真的!如果不行请随 ...
- [Leetcode][程序员面试金典][面试题17.13][JAVA][恢复空格][动态规划][Trie][字符串哈希]
[问题描述][中等] [解答思路] 1. 动态规划 动态规划流程 第 1 步:设计状态 dp[i] 表示字符串的前 i 个字符的最少未匹配数. 第 2 步:状态转移方程 假设当前我们已经考虑完了前 i ...
最新文章
- 零基础学习java软件开发攻略
- Nature综述——真菌的多样性:真菌的高通量测序及鉴定
- (二) shiro入门 :输出 hello world
- “您的Microsoft Internet Explorer浏览器包含最新版本的内置Adobe Flash Player“解决
- 圆的面积公式的巧妙推导
- 最新版MySQL在MacOS上的实践!
- MySQL 优化 —— EXPLAIN 执行计划详解
- 2019年IT界,程序员是否不好找工作了?
- 博文翻译系列——如何入门数据科学 without spending a penny
- IEEE发布AI三原则:机器人革命中需要保护人类福祉 | 138页报告
- C++面向对象程序设计课程笔记(第三周)
- WPF使用DialogResult.OK报错
- ASP站内搜索代码#
- Golang 信息采集
- unity怎么制作云飘动_Unity Shader教程之 旗帜飘扬效果的简单实现
- 使用python将doc的word文件转换成docx文件
- 简单的指针二叉查找树和数组二叉查找树
- CANoe/CAPL ,钉钉群助手消息通知
- 《我爱我家》主创重聚北京台春晚 宋丹丹感谢英达
- 阿里矢量图在uniapp中的使用