Hash,一般翻译做散列、杂凑,或音译为哈希,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,而不可能从散列值来唯一的确定输入值。简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。

这里就只介绍一种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

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
 
Source
 
题目大意:A串在B串出现了多少次 

看代码:

#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函数学习)的更多相关文章

  1. leetcode 入门第一题 4ms? 8ms? Two Sum

    今天开启leetcode 入门第一题 题意很简单,就是一个数组中求取两数之和等于目标数的一对儿下标 1.暴力 n^2 两个for循环遍历 用时0.1s 开外 代码就不用写了 2.二分 nlogn 我们 ...

  2. CTF---Web入门第一题 what a fuck!这是什么鬼东西?

    what a fuck!这是什么鬼东西?分值:10 来源: DUTCTF 难度:易 参与人数:7942人 Get Flag:3358人 答题人数:3475人 解题通过率:97% what a fuck ...

  3. CTF---编程入门第一题 循环

    循环分值:10 来源: 北邮天枢战队 难度:易 参与人数:1478人 Get Flag:467人 答题人数:523人 解题通过率:89% 给出一个循环公式,对于一个整数n,当n为奇数时,n=3n+1, ...

  4. CTF---隐写术入门第一题 SB!SB!SB!

    SB!SB!SB!分值:20 来源: 西普学院 难度:中 参与人数:4913人 Get Flag:1541人 答题人数:1577人 解题通过率:98% LSB 解题链接: http://ctf5.sh ...

  5. CTF---安全杂项入门第一题 丘比龙的最爱

    丘比龙的最爱分值:10 来源: 2014HCTF 难度:易 参与人数:4498人 Get Flag:1366人 答题人数:1384人 解题通过率:99% 传说,丘比龙是丘比特的弟弟,丘比龙是一只小爱神 ...

  6. CTF---密码学入门第一题 这里没有key

    这里没有key分值:10 来源: 西普学院 难度:易 参与人数:5577人 Get Flag:1965人 答题人数:2074人 解题通过率:95% 你说没有就没有啊,俺为啥要听你的啊 解题链接: ht ...

  7. #000 Python 入门第一题通过扩展,学到了更多的知识

    #1写在前面的话 我觉得这样学习或许能够在学习的过程中事半功倍 第一道简单的python编写代码输出10行带标号的“Hello,world.”,具体效果参阅输入输出示例 1:Hello,world. ...

  8. 状压dp入门第一题 poj3254

    题目链接 http://poj.org/problem?id=3254 转自http://blog.csdn.net/harrypoirot/article/details/23163485 #inc ...

  9. HDU 1880 字符串hash 入门题

    Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...

随机推荐

  1. 操作系统--UNIX代码段和数据段分开

    (1)代码段:代码段是用来存放可执行文件的操作指令,也就是说是它是可执行程序在内存中的镜像.代码段需要防止在运行时被非法修改,所以只准许读取操作,而不允许写入(修改)操作----它是不可写的. (2) ...

  2. Android简单的monkey测试

    Android中的monkey测试是比较常用的工具了,设定好monkey之后,让手机跑一晚上,第二天分析日志,这样能更加有效率的工作. monkey测试的工具比较多,基本的方法都差不多. 抓取日志: ...

  3. 解决Mac版微信小程序开发工具打开后无法显示二维码

    问题描述: 正常情况下,打开微信小程序开发工具后,首页提示扫描二维码进行登陆,但是如果不显示二维码,当然无法登陆. 解决方案: 无法显示二维码肯定是程序运行哪里出错了,我们直接点击桌面图标是无法排查错 ...

  4. [转]MySQL时间与字符串相互转换

    转至:https://www.cnblogs.com/wangyongwen/p/6265126.html 时间.字符串.时间戳之间的互相转换很常用,但是几乎每次使用时候都喜欢去搜索一下用法:本文整理 ...

  5. 存储过程自动更新ID

    DECLARE @i int --更新题序编号 UPDATE UserAnswer SET @i=@i+,TestOrder=@i WHERE UserScoreID=' //根据ID 累加更新

  6. javax.servlet.http.httpServletRequest接口

    HttpServletRequest接口中常用的方法:   - String getContentPath();//获取webapp根目录路径,如下图:   下面研究request到底是一个怎样的范围 ...

  7. Metasploit 读书笔记-神器Meterpreter

    一、基本命令 截屏 screenshot 2.获取系统平台信息 sysinfo 3.进程信息 ps 4.获取键盘记录 查看进程信息ps--migrate将会话迁移至explorer.exe进程空间中- ...

  8. 模拟使用zookeeper实现master选举

    1.模拟选举机器类 package com.karat.cn.zookeeperAchieveLock.zkclient; import java.io.Serializable; /** * 选举的 ...

  9. python中各种转义字符

    转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \’ 单引号 \” 双引号 \a 响铃 \b 退格(Backspace) \e 转义 \000 空 \n 换行 \v 纵向制表符 \t 横向制 ...

  10. POJ 2421 Constructing Roads(最小生成树)

    Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...