字符串hash

https://blog.csdn.net/pengwill97/article/details/80879387

https://blog.csdn.net/chaiwenjun000/article/details/71079819

AC代码

#include <cmath>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
typedef unsigned long long ull;
const int maxn=1e6+;
const int base=; //base,基数 ull power[maxn]; //存储b^n
void init()
{
power[]=; //base^0=1
for(int i=; i<maxn; i++)
power[i]=power[i-]*base; //对ull取模
} char s1[maxn],s2[maxn];
ull Hash[maxn]; //记录主串哈希值的数组 int main()
{
init();
int T;
cin>>T;
while(T--)
{
scanf("%s%s",s1+,s2+); //从第一位开始输入
int n=strlen(s1+),m=strlen(s2+);
Hash[]=; //记录主串哈希值的数组
for(int i=; i<=m; i++) //计算主串的滚动Hash值
Hash[i]=Hash[i-]*base+(ull)(s2[i]-'A'+);
ull s=;
for(int i=; i<=n; i++) //计算匹配串的Hash值
s=s*base+(ull)(s1[i]-'A'+);
int ans=;
for(int i=; i<=m-n; i++)
if(s==Hash[i+n]-Hash[i]*power[n])
ans++;
printf("%d\n",ans);
}
return ;
}

poj 3461 hash解法的更多相关文章

  1. POJ 3461 Oulipo

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  2. POJ 3461 Oulipo(乌力波)

    POJ 3461 Oulipo(乌力波) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] The French autho ...

  3. HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)

    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...

  4. POJ 3461 Oulipo[附KMP算法详细流程讲解]

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  5. poj 3461 字符串单串匹配--KMP或者字符串HASH

    http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include ...

  6. 字符串hash - POJ 3461 Oulipo

    Oulipo Problem's Link ---------------------------------------------------------------------------- M ...

  7. POJ 3461 Oulipo(字符串hash)

    题目链接 字符串hash判断字符串是否相等. code #include<cstdio> #include<algorithm> #include<cstring> ...

  8. POJ 3274 HASH

    题目链接:http://poj.org/problem?id=3274 题意+思路: 点击这里 补充:因为有减法运算,所以可能会造成运算后结果为负数,所以需要把结果统一转换成正数[不然数组下标访问不到 ...

  9. POJ 3349 HASH

    题目链接:http://poj.org/problem?id=3349 题意:你可能听说话世界上没有两片相同的雪花,我们定义一个雪花有6个瓣,如果存在有2个雪花相同[雪花是环形的,所以相同可以是旋转过 ...

随机推荐

  1. UEditor中多图上传的bug

    多图上传 预览:支持浏览器版本  IE8以上 在线管理:由于存在bug,显示不了 ueditor-1.1.1.jar解压后找到FileManager 1.修改com.baidu.ueditor.hun ...

  2. POJ 2255 Tree Recovery——二叉树的前序遍历、后序遍历、中序遍历规则(递归)

    1.前序遍历的规则:(根左右) (1)访问根节点 (2)前序遍历左子树 (3)前序遍历右子树 对于图中二叉树,前序遍历结果:ABDECF 2.中序遍历的规则:(左根右) (1)中序遍历左子树 (2)访 ...

  3. CF-1100 E Andrew and Taxi

    CF-1100E Andrew and Taxi https://codeforces.com/contest/1100/problem/E 知识点: 二分 判断图中是否有环 题意: 一个有向图,每边 ...

  4. 【搜索 ex-BFS】bzoj2346: [Baltic 2011]Lamp

    关于图中边权非零即一的宽度优先搜索 Description 译自 BalticOI 2011 Day1 T3「Switch the Lamp On」有一种正方形的电路元件,在它的两组相对顶点中,有一组 ...

  5. 用python实现自动玩21点小游戏

    1. 背景 前段时间发现一个论坛上(https://npupt.com/blackjack.php)有21点小游戏. 这个21点小游戏的规则是每个人开局都会获得随机点数,如果觉得点数小,可以继续摸牌. ...

  6. 【linux】【git】git报错fatal: HTTP request failed

    在使用git pull.git push.git clone会报类似如下的错误: error: The requested URL returned error: 401 Unauthorized w ...

  7. kafka异常问题汇总

    1.报错:: java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.NotLeaderForPartition ...

  8. BFS:UVa201-Squares

    Squares A children's board game consists of a square array of dots that contains lines connecting so ...

  9. python常见陷阱

    copy to https://pythonguidecn.readthedocs.io/zh/latest/writing/gotchas.html 大多数情况下,Python的目标是成为一门简洁和 ...

  10. linux 搭建apache 服务器

    1.查看apache服务器 /etc/init.d/httpd status 若没有,则使用yum  -y install httpd  安装软件 2.设置开机启动 chkconfig httpd o ...