A - Number Sequence 哈希算法(例题)
InputThe first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a[N]. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000].
OutputFor each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
Sample Input
2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1
Sample Output
6
-1
哈希算法的模板题,题目数据没有当 n < m 的情况
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=1E6+;
const int M=1E4+;
const int base =;
const int mod=1e9+;
typedef unsigned long long ll;
ll a[N],b[M];
ll ha[N],p[N]; ll gethash(ll x,ll y){
return (ha[y]%mod-(ha[x-]%mod*p[y-x+]%mod)%mod+mod)%mod;
} int main(){
int t;
// cin>>t;
scanf("%d",&t);
while(t--){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
ll n,m;
scanf("%lld%lld",&n,&m);
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=;i<=m;i++)
scanf("%lld",&b[i]);
p[]=;
if(n>=m){
for(int i=;i<=n;i++){
ha[i]=((ha[i-]%mod*base)%mod+a[i])%mod;
p[i]=(p[i-]%mod*base)%mod;
}
ll ans=;
for(int i=;i<=m;i++) ans=(ans%mod*base+b[i])%mod;
int pos=;
for(int i=m;i<=n;i++){
if(gethash(i-m+,i)==ans){
pos=i-m+;
break;
}
}
if(pos==) pos=-;
printf("%d\n",pos);
}
// else {
// for(int i=1;i<=m;i++){
// ha[i]=((ha[i-1]%mod*base)%mod+b[i])%mod;
// p[i]=(p[i-1]%mod*base)%mod;
// }
// ll ans=0;
// for(int i=1;i<=n;i++) ans=(ans%mod*base+a[i])%mod;
// int pos=0;
// for(int i=n;i<=m;i++){
// if(gethash(i-n+1,i)==ans){
// pos=i-n+1;
// break;
// }
// }
// if(pos==0) pos=-1;
// printf("%d\n",pos);
// }
}
return ;
}
A - Number Sequence 哈希算法(例题)的更多相关文章
- hdu_1711: Number Sequence【KMP算法】
题目链接 此次插播点笔记 hdu中点击蓝色的"Compilation Error"可以查看自己是为什么CE的 hdu中提交的话,语言选择G++可以使用<bits/stdc++ ...
- HDU1711 Number Sequence 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目大意:最基础的字符串匹配,只不过这里用整数数组代替了字符串. 给你两个数组 \(a[1..N ...
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- Rabin_Karp(hash) HDOJ 1711 Number Sequence
题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...
- Number Sequence ----HDOJ 1711
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 一致性哈希算法(consistent hashing)样例+測试。
一个简单的consistent hashing的样例,非常easy理解. 首先有一个设备类,定义了机器名和ip: public class Cache { public String name; pu ...
- HDU1005 Number Sequence (奇技淫巧模拟)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mo ...
- os常用模块,json,pickle,shelve模块,正则表达式(实现运算符分离),logging模块,配置模块,路径叠加,哈希算法
一.os常用模块 显示当前工作目录 print(os.getcwd()) 返回上一层目录 os.chdir("..") 创建文件包 os.makedirs('python2/bin ...
- 字符串哈希算法(以ELFHash详解)
更多字符串哈希算法请参考:http://blog.csdn.net/AlburtHoffman/article/details/19641123 先来了解一下何为哈希: 哈希表是根据设定的哈希函数H( ...
随机推荐
- 大多数人不知道的表格其他写法的onmouseover效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- SpringBoot2 整合 Swagger2
SpringBoot2 整合 Swagger2 SpringBoot整合三板斧 第一步.引入pom <dependency> <groupId>com.spring4all&l ...
- Servlet(二)----注解配置
## Servlet3.0 * 好处: * 支持注解配置.可以不需要web.xml了. * 步骤: 1.创建JavaEE项目,选择Servlet的版本3.0以上,可以不创建web.xml 2. ...
- 关于Anaconda安装以后使用Jupyter Notebook无法直接打开浏览器的解决方法
关于Anaconda安装以后使用Jupyter Notebook无法直接打开浏览器的解决方法 1.首先打开Anoconda Prompt,输入命令 jupyter notebook --generat ...
- Transformers 中使用 TorchScript | 四
作者|huggingface 编译|VK 来源|Github 注意:这是我们使用TorchScript进行实验的开始,我们仍在探索可变输入大小模型的功能.它是我们关注的焦点,我们将在即将发布的版本中加 ...
- 从零开始实现穿衣图像分割完整教程(附python代码演练)
时装业是人工智能领域很有前景的领域.研究人员可以开发具有一定实用价值的应用.我已经在这里展示了我对这个领域的兴趣,在那里我开发了一个来自Zalando在线商店的推荐和标记服装的解决方案. 在这篇文章中 ...
- Hive学习笔记七
目录 函数 一.系统自带函数 二.自定义函数 三.自定义UDF函数开发案例 压缩和存储 一.Hadoop源码编译支持Snappy压缩 1.资源准备 2.jar包安装 3.编译源码 二.Hadoop压缩 ...
- 事务框架之声明事务(自动开启,自动提交,自动回滚)Spring AOP 封装
利用Spring AOP 封装事务类,自己的在方法前begin 事务,完成后提交事务,有异常回滚事务 比起之前的编程式事务,AOP将事务的开启与提交写在了环绕通知里面,回滚写在异常通知里面,找到指定的 ...
- python—json
一.json数据类型:是字符串 # json通用数据类型,所有语言都可以用 # {k-v}形式存在,里面只能用双引号"" # 定义json字符串,要用'''{}'''格式 #htt ...
- 求你了,别再问我Zookeeper如何实现分布式锁了!!!
导读 真是有人(锁)的地方就有江湖(事务),今天不谈江湖,来撩撩人. 分布式锁的概念.为什么使用分布式锁,想必大家已经很清楚了.前段时间作者写过Redis是如何实现分布式锁,今天这篇文章来谈谈Zook ...