poj 3461 hash解法
字符串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解法的更多相关文章
- POJ 3461 Oulipo
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3461 Oulipo(乌力波)
POJ 3461 Oulipo(乌力波) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] The French autho ...
- 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 ...
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- poj 3461 字符串单串匹配--KMP或者字符串HASH
http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include ...
- 字符串hash - POJ 3461 Oulipo
Oulipo Problem's Link ---------------------------------------------------------------------------- M ...
- POJ 3461 Oulipo(字符串hash)
题目链接 字符串hash判断字符串是否相等. code #include<cstdio> #include<algorithm> #include<cstring> ...
- POJ 3274 HASH
题目链接:http://poj.org/problem?id=3274 题意+思路: 点击这里 补充:因为有减法运算,所以可能会造成运算后结果为负数,所以需要把结果统一转换成正数[不然数组下标访问不到 ...
- POJ 3349 HASH
题目链接:http://poj.org/problem?id=3349 题意:你可能听说话世界上没有两片相同的雪花,我们定义一个雪花有6个瓣,如果存在有2个雪花相同[雪花是环形的,所以相同可以是旋转过 ...
随机推荐
- Feign-请求不同注册中心的服务
场景 需要通过Feign Client请求,其他注册中心或者其他Restful服务. 临时方案 Feign 请求转为RestTemplate http请求. 优点:能适应,feign环境和非feign ...
- non-JRMP server at remote endpoint
#在相应的domain的domain.xml文件添加下面红色设置,并重启domain <admin-service system-jmx-connector-name="system& ...
- iterator方法和for方法 遍历数据库user表结果集ResultSet
首先,把连接数据库的语句做成工具类,因为会一直用到这几句 代码如下: package com.swift.jdbc; import java.sql.Connection; import java.s ...
- url地址数据参数转化JSON对象(js三种方法实现)
当我们用get方法提交表单时,在url上会显示出请求的参数组成的字符串,例如:http://localhost:3000/index.html?phone=12345678901&pwd=12 ...
- 【搜索 技巧】Letter gaps
需要一定技巧的搜索题 题目描述 记得做过这样一道题 Ponder This Challenge: In the string CABACB, each letter appears exactly t ...
- Web框架之Django_09 重要组件(Django中间件、csrf跨站请求伪造)
摘要 Django中间件 csrf跨站请求伪造 一.Django中间件: 什么是中间件? 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于 ...
- Python基础知识-day2
格式化输出 %占位符,s字符串,d 数字, 表示% 用%% name = input("请输入姓名: ") age = input("请输入年龄: ") he ...
- timer event
/* linux/kernel/time/jiffies.c*/ static cycle_t jiffies_read(struct clocksource *cs) { return (cycle ...
- c++ 快速幂 代码实现
懒得打代码系列… 不过这个代码挺短的死背下来也ok 解析在最下面 建议自己手动试个数据理解一下 比如 3^5 ^^ 原理:a ^ b = a ^ (b / 2) * 2 (b是奇数的话还要再乘一个a) ...
- 出现Android.os.NetworkOnMainThreadException 错误
两种方法解决: 1.如果用的gradle打包,在build.gradle中修改配置 修改SDKVersion 为低版本(7),不能版本降低过多,否则会出现很多不适配. 2.将网络访问放在一个新的线程中 ...