Oulipo(Hash入门第一题 Hash函数学习)
这里就只介绍一种Hash函数,其实有好多种,但是目前只学了这一种。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686
Oulipo
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 26635 Accepted Submission(s): 10252
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.
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.
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
3
0
看代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
typedef unsigned long long ULL;
const int maxn=1e6+;
const ULL mod=1e9+;
const ULL Ha=;
ULL xp[maxn];
ULL Hash1[maxn],Hash2[maxn];
void Init()//xp[i] 等于Ha^i 为了后面的计算(看不懂先接着看)
{
xp[]=;
for(int i=;i<maxn;i++) xp[i]=xp[i-]*Ha;
return ;
}
void make_Hash(string s,ULL Hash3[])//给一个串每个位置一个Hash值
{
int len=s.size();
Hash3[len]=;
for(int i=len-;i>=;i--)
{
Hash3[i]=(Hash3[i+]*Ha+(s[i]-'A'+));
}
return ;
}
ULL get_Hash(ULL n,ULL len,ULL Hash[])//得到那个子串的Hash值
{ return (Hash[n]-Hash[n+len]*xp[len]);//这里值得思考一下 为什么*xp[len]呢? 最后你会发现这样子处理得到的结果可以解决第一个串出现在中间的情况
}
int main()
{
Init();
string s1,s2;
int T;
scanf("%d",&T);
while(T--)
{
ULL ans=;
//scanf("%s",s1,s2);
cin>>s1>>s2;
make_Hash(s1,Hash1);
make_Hash(s2,Hash2);
ULL len1=s1.size();
ULL len2=s2.size();
ULL tmp=get_Hash(,len1,Hash1);
//cout<<"tmp "<<tmp<<endl;
for(int i=;i+len1-<len2;i++)//直接取出相同长度的出来比较就好了
{
//cout<<i<<" "<<get_Hash(i,len1,Hash2)<<endl;
if(get_Hash(i,len1,Hash2)==tmp) ans++;
} printf("%llu\n",ans);
}
return ;
}
Oulipo(Hash入门第一题 Hash函数学习)的更多相关文章
- leetcode 入门第一题 4ms? 8ms? Two Sum
今天开启leetcode 入门第一题 题意很简单,就是一个数组中求取两数之和等于目标数的一对儿下标 1.暴力 n^2 两个for循环遍历 用时0.1s 开外 代码就不用写了 2.二分 nlogn 我们 ...
- CTF---Web入门第一题 what a fuck!这是什么鬼东西?
what a fuck!这是什么鬼东西?分值:10 来源: DUTCTF 难度:易 参与人数:7942人 Get Flag:3358人 答题人数:3475人 解题通过率:97% what a fuck ...
- CTF---编程入门第一题 循环
循环分值:10 来源: 北邮天枢战队 难度:易 参与人数:1478人 Get Flag:467人 答题人数:523人 解题通过率:89% 给出一个循环公式,对于一个整数n,当n为奇数时,n=3n+1, ...
- CTF---隐写术入门第一题 SB!SB!SB!
SB!SB!SB!分值:20 来源: 西普学院 难度:中 参与人数:4913人 Get Flag:1541人 答题人数:1577人 解题通过率:98% LSB 解题链接: http://ctf5.sh ...
- CTF---安全杂项入门第一题 丘比龙的最爱
丘比龙的最爱分值:10 来源: 2014HCTF 难度:易 参与人数:4498人 Get Flag:1366人 答题人数:1384人 解题通过率:99% 传说,丘比龙是丘比特的弟弟,丘比龙是一只小爱神 ...
- CTF---密码学入门第一题 这里没有key
这里没有key分值:10 来源: 西普学院 难度:易 参与人数:5577人 Get Flag:1965人 答题人数:2074人 解题通过率:95% 你说没有就没有啊,俺为啥要听你的啊 解题链接: ht ...
- #000 Python 入门第一题通过扩展,学到了更多的知识
#1写在前面的话 我觉得这样学习或许能够在学习的过程中事半功倍 第一道简单的python编写代码输出10行带标号的“Hello,world.”,具体效果参阅输入输出示例 1:Hello,world. ...
- 状压dp入门第一题 poj3254
题目链接 http://poj.org/problem?id=3254 转自http://blog.csdn.net/harrypoirot/article/details/23163485 #inc ...
- HDU 1880 字符串hash 入门题
Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...
随机推荐
- c/c++头文件中#ifndef/#define/#endif的用法
想必很多人都看过“头文件中用到的 #ifndef/#define/#endif 来防止该头文件被重复引用”.但是是否能理解“被重复引用”是什么意思?头文件被重复引用了,会产生什么后果?是不是所有的头文 ...
- 数据结构 hbb(汉堡包)
数据结构 hbb(汉堡包) 问题描述 汉堡包有收集汉犇犇的癖好,它喜欢把汉犇犇一个叠一个的放置. 因为它有强迫症,所以每当它想放一个新的汉犇犇进去的时候并不一定想直接叠在最上面,简单的说,当他想放第 ...
- LeetCode第21题:合并两个有序链表
题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1- ...
- C#中 ACCESS数据库常用操作语句...容易出错的地方(DateTime类型)
这次在C#编程过程中,第一次用到了ACCESS数据库,重点涉及到时间类型,整数类型.是否类型....;遇到了许多困难,就把这些整理了下来,与大家分享. 一.Insert语句的基本格式: INSERT ...
- php+mysql入门
mysql+php+apache可以快速的架构动态网站. 首先,为什么php容易搞mysql,因为与mysql交互成为了php语言的一种特性. 一.mysql入门 mysql是一种开源的关系型数据库. ...
- SOA架构之限流
参考: 服务限流 1. 限流的作用 限流主要的作用是保护服务节点或者集群后面的数据节点,防止瞬时流量过大使服务和数据崩溃(如前端缓存大量实效),造成不可用:还可用于平滑请求. 2. 限流算法 限流算法 ...
- 解决“System.Data.OracleClient 需要 Oracle 客户端软件 version 8.1.7 或更高版本”的问题
以server2008为例: 首先确保使用sqlplus能访问数据库. 1.管理工具->计算机管理->本地用户和组->组->administrators属性,添加,高级,立即查 ...
- ThreadLocalRandom原理
原文链接:https://www.jianshu.com/p/9c2198586f9b 2.2. 并发包中ThreadLocalRandom类原理剖析 ThreadLocalRandom类是JDK7在 ...
- SP263 PERIOD - Period KMP技巧
\(\color{#0066ff}{题目描述}\) 如果一个字符串S是由一个字符串T重复K次形成的,则称T是S的循环元.使K最大的字符串T称为S的最小循环元,此时的K称为最大循环次数. 现给一个给定长 ...
- webpack 的使用教程
webpack 的使用教程 今天接触webpack,就着官网上的教程一步一步的玩,把自己的理解总结以便和大家交流 webpack的主要特点 1. 可以把js,css,image,甚至文本当成模块来处理 ...