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 ...
随机推荐
- python获取csv文本的某行或某列数据
#coding = 'utf-8' import csv # 使用list,只能读取列,而且是全文读取,csv.reader会自动把CSV内容生成数组 ''' df = csv.reader(open ...
- Python文档操作
1.打开和关闭文件 open('C:\Users\Second One\Desktop\a.txt')文件路径必须完整路径且为字符串格式 有三种方式: open('C:\\Users\\Second ...
- python 并发编程 多进程 目录
python multiprocessing模块 介绍 python 开启进程两种方法 python 并发编程 查看进程的id pid与父进程id ppid python 并发编程 多进程 Proce ...
- python 并发编程 查看进程的id pid与父进程id ppid
查看进程id pid 不需要传参数 from multiprocessing import Process import time import os def task(): print(" ...
- Cassandra视图
一.简介 Cassandra作为一个P2P结构的NOSQL数据库,使用与HBase不同的去中心化架构,在国外使用非常广泛,受欢迎程度甚至在Hbase之上.今天这篇文章介绍Cassandra在视图方面设 ...
- java8----Predicate接口的使用
//5.lambda表达式中加入Predicate // 甚至可以用and().or()和xor()逻辑函数来合并Predicate, // 例如要找到所有以J开始,长度为四个字母的名字,你可以合并两 ...
- iconv编码转换报错问题
今天,再由ISO-8859编码格式转化为UTF-8格式过程中,出现报错:iconv: 未知 10304 处的非法输入序列. 问题分析:ISO-8859是英文格式的编码方式,不支持中文,为了解决中文支持 ...
- RabbitMq学习2-php命令行模式测试rabbitmq
一.RabbitMQ结构 1.几个概念说明: Broker:简单来说就是消息队列服务器实体. Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列. Queue:消息队列载体 ...
- redis学习之旅-初识Redis
定义: redis是一种支持Key-Value等多种数据结构的存储系统.可用于缓存,事件发布或订阅,高速队列等场景.该数据库使用ANSI C语言编写,支持网络,提供字符串,哈希,列表,队列,集合结构直 ...
- PyCharm控制台python shell 和 IPython shell的切换
1. IPython介绍 IPython 是一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许 ...