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( ...
随机推荐
- 条件随机场 CRF
2019-09-29 15:38:26 问题描述:请解释一下NER任务中CRF层的作用. 问题求解: 在做NER任务的时候,神经网络学习到了文本间的信息,而CRF学习到了Tag间的信息. 加入CRF与 ...
- [最短路,floyd] Codeforces 1204C Anna, Svyatoslav and Maps
题目:http://codeforces.com/contest/1204/problem/C C. Anna, Svyatoslav and Maps time limit per test 2 s ...
- [单调栈]小A的柱状图
链接:https://ac.nowcoder.com/acm/problem/23619来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...
- mysql事务提交和回滚机制
应用场景: 银行取钱,从ATM机取钱,分为以下几个步骤 1 登陆ATM机,输入密码: 2 连接数据库,验证密码: 3 验证成功,获得用户信息,比如存款余额等: 4 用 ...
- UVA11987 Almost Union-Find 并查集的节点删除
题意: 第一行给出一个n,m,表示 n个集合,里面的元素为1~n,下面有m种操作,第一个数为 1 时,输入a,b 表示a,b 两个集合合并到一起,第一个数为 2 时,输入a,b表示将 a 从他原来的集 ...
- centos7中提升用户权限
提升用户权限我看网上资源有两种方法,一种是修改/etc/sudoers/文件将新增的用户权限提升为和root一样的权限,这种方法不知道怎么回事我没用应用成功,这里我介绍第二种方法,第二种方法是更改/e ...
- git原理,git命令使用详解,github使用 --有此图文并茂原来如此简单
一.Git分布式控制系统原理:git有三个区,被管理的代码或文件是从:工作区-->暂存区-->本地版本库. 二.GitHub创建线上仓库GitHub是一个面向开源及私有软件项目的托管平台, ...
- 在线诊断工具arthas (windows)
介绍: arthas是阿里巴巴开发的一款开源的,Java应用程序排查问题的非常好用的工具 当你遇到以下类似问题而束手无策时 arthas 可以帮助你解决: 这个类从哪个 jar 包加载的?为什么会报各 ...
- IDEA 新版本激活之后老是有弹窗 解决方法
用了最新的版本的IDEA,然后用网上的方法破解到了2089年,但是打开IDEA的时候,老是出现一个弹窗,内容如下: This agent is for learning and research pu ...
- IO 流
------------恢复内容开始------------ 一.流的简介 输入流:把硬盘中的数据读取到内存中 输出流:把内存中的数据读取到硬盘中 1字符=2字节 1字节=8位 1.2顶级父类 二. ...