Oulipo【Hash】
Oulipo
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 45515 | Accepted: 18181 |
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
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- int n,k,len1,len2;
- int next1[];
- char s1[];
- char s2[];
- long long HASH[];
- long long E[],M=;
- long long EE = ;
- int init()
- {
- long long Key=;
- int ans=;
- memset(E,,sizeof(E));
- memset(HASH,,sizeof(HASH));
- E[len2]=;
- for (int i=len2-;i>=;i--)
- E[i]=E[i+]*EE%M;
- for (int i=;i<=len2;i++)
- HASH[]=(HASH[]+E[i]*(s1[i-]))%M;
- for (int i=;i<=len2;i++)
- Key=(Key+E[i]*(s2[i-]))%M;
- if (HASH[]==Key) ans++;
- for (int i=;i<=len1-len2+;i++)
- {
- HASH[i]=(HASH[i-]-s1[i-]*E[]%M+M)%M*EE%M+s1[i-+len2];
- if (HASH[i]==Key) ans++;
- }
- printf("%d\n",ans);
- }
- int main(){
- int n;
- cin >> n;
- for (int i=;i<=n;i++){
- scanf("%s",s2) ;
- scanf("%s",s1) ;
- len1=strlen(s1);
- len2=strlen(s2);
- init();
- }
- return ;
- }
Oulipo【Hash】的更多相关文章
- 【hash】BZOJ3751-[NOIP2014]解方程
[题目大意] 已知多项式方程:a0+a1*x+a2*x^2+...+an*x^n=0.求这个方程在[1,m]内的整数解(n和m均为正整数). [思路] *当年考场上怒打300+行高精度,然而没骗到多少 ...
- 【hash】Power Strings
[题意]: 给出s串出来,能否找到一个前缀 ,通过多次前缀进行拼接.构成s串.如果有多个,请输出最多次数那个. 如:aaaa 可以用1个a,进行4次拼接 可以用2个a,进行2次拼接 可以用4个a,进行 ...
- 【hash】Similarity of Subtrees
图片来源: https://blog.csdn.net/dylan_frank/article/details/78177368 [题意]: 对于每一个节点来说有多少对相同的子树. [题解]: 利用层 ...
- 【hash】Seek the Name, Seek the Fame
[哈希和哈希表]Seek the Name, Seek the Fame 题目描述 The little cat is so famous, that many couples tramp over ...
- 【hash】A Horrible Poem
[题目链接] # 10038. 「一本通 2.1 练习 4」A Horrible Poem [参考博客] A Horrible Poem (字符串hash+数论) [题目描述] 给出一个由小写英文字母 ...
- 【hash】Three friends
[来源]:bzoj3916 [参考博客] BZOJ3916: [Baltic2014]friends [ 哈希和哈希表]Three Friends [Baltic2014][BZOJ3916]frie ...
- 湖南师范大学2018年大学生程序设计竞赛新生赛 A 齐神和心美的游戏【hash】
[链接]:A [题意]:给你n个数的序列和k.判断是否可以三个数组成k(同一个数可以拿多次) [分析]:每个数vis记录一下.2层循环.两数之和不超过k以及剩下的数出现在序列中那么ok. [代码]: ...
- 【hash】珍珠
[来源] https://loj.ac/problem/2427 [参考博客] LOJ#2427. 「POI2010」珍珠项链 Beads [题解]: 复杂度计算: 暴力枚举k每次计算是n/2+n/3 ...
- 洛谷P1117 优秀的拆分【Hash】【字符串】【二分】【好难不会】
题目描述 如果一个字符串可以被拆分为AABBAABB的形式,其中 A和 B是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串aabaabaaaabaabaa,如果令 A=aabA ...
随机推荐
- 测试需要了解的技术之基础篇三__持续集成持续交付DevOps
持续集成.持续交付.DevOps 1.容器技术Docker:容器技术介绍.Docker安装与加速配置.Docker基础命令.Docker搭建selenium.Docker搭建持续集成平台Jenkins ...
- lnmp一键安装包卸载mysql,重新安装报错mysql57-community-release conflicts with mysql-community-release-el6-5.noarch
环境:CentOS Linux release 7.6.1810 lnmp1.5 独立下载mysql仓库 wget -i -c http://dev.mysql.com/get/mysql57-com ...
- HDU4372(第一类斯特林数)
题意:N座高楼,高度均不同且为1~N中的数,从前向后看能看到F个,从后向前看能看到B个,问有多少种可能的排列数. 0 < N, F, B <= 2000 首先我们知道一个结论:n的环排列的 ...
- 20191118 Spring Boot官方文档学习(4.8)
4.8. RSocket RSocket参考 RSocket是用于字节流传输的二进制协议.它通过单个连接传递的异步消息来启用对称交互模型. Spring框架的spring-messaging模块在客户 ...
- input函数以及while处理列表和字典
一.函数input()的工作原理 .input()函数:获取输入的字符串 示例: message = input('请输入信息,方便电脑显示') print(message) print('您输入的信 ...
- 前端 CSS 盒子模型 目录
CSS盒子模型介绍 padding border属性
- [Web 前端] 032 vue 初识
目录 0. 先下载 1. 先写个轮廓 2. 牛刀小试 2.1 例子 1 2.2 例子 2 3. 模板语法 上例子 4. 文本指令 上例子 5. 属性操作 上例子 6. 样式操作 上例子 类名的操作 s ...
- webpack e6转化成es5 配置方法
方法一: https://www.babeljs.cn/setup#installation 按照babel官方的配置配 方法二: https://www.jianshu.com/p/ce28cedd ...
- 计算机系统结构总结_Cache Optimization
Textbook: <计算机组成与设计——硬件/软件接口> HI <计算机体系结构——量化研究方法> QR Ch4. Cache Optimization 本章要 ...
- asp.net Excel导入和导出
1.Excel数据导入到数据库中: //该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径,sheetname为表示那个Excel表: p ...