字符串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. Java InputStream、String、File相互转化

    String --> InputStreamByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); Inp ...

  2. java集合测试类等

    package demo.mytest; import java.lang.ref.SoftReference;import java.lang.ref.WeakReference;import ja ...

  3. ios之alloc和init

      复制代码 SomeObject *obj = [[SomeObject alloc] initWithCenter:centerPoint radius:radius]; 和   复制代码 Som ...

  4. (13)zabbix External checks 外部命令检测

    1.  概述 zabbix server运行脚本或者二进制文件来执行外部检测,外部检测不需要在被监控端运行任何agentd item key语法如下: ARGUMENT DEFINITION scri ...

  5. Python9-文件操作-day8

    # 1.文件路径:d:\xxx.txt# 绝对路径:从根目录开始# 想对路径:当前文件下# 2.编码方式:utf-8/gbk...# 3.操作方式:只读.只写.追加.读写.写读..... f=open ...

  6. 剑指Offer(书):反转链表

    题目:输入一个链表,反转链表后,输出新链表的表头. 分析:要分清他的前一个节点和后一个节点,开始的时候前节点为null,后节点为head.next,之后,反转. public ListNode Rev ...

  7. 基于链式链表的栈链式存储的C风格实现

    链式链表的头文件与CPP文件见前文 头文件: #ifndef _LINKSTACK_H_ #define _LINKSTACK_H_ typedef void LinkStack; //创建一个栈 L ...

  8. BZOJ 4809: 皇后

    题目大意: n皇后问题,有些格子不能放. 题解: 直接暴力,并不用加优化就能过. 代码: #include<cstdio> using namespace std; int cc,n,an ...

  9. linux python 安装 pip出现 No module named 'setuptools'

    1.下载pip wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#m ...

  10. [uiautomator篇] bluetooth---接口来做

    package com.softwinner.performance.frameratetest; import android.Manifest; import android.bluetooth. ...