Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one. 

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 哈希算法(例题)的更多相关文章

  1. hdu_1711: Number Sequence【KMP算法】

    题目链接 此次插播点笔记 hdu中点击蓝色的"Compilation Error"可以查看自己是为什么CE的 hdu中提交的话,语言选择G++可以使用<bits/stdc++ ...

  2. HDU1711 Number Sequence 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目大意:最基础的字符串匹配,只不过这里用整数数组代替了字符串. 给你两个数组 \(a[1..N ...

  3. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  4. Rabin_Karp(hash) HDOJ 1711 Number Sequence

    题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...

  5. Number Sequence ----HDOJ 1711

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. 一致性哈希算法(consistent hashing)样例+測试。

    一个简单的consistent hashing的样例,非常easy理解. 首先有一个设备类,定义了机器名和ip: public class Cache { public String name; pu ...

  7. 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 ...

  8. os常用模块,json,pickle,shelve模块,正则表达式(实现运算符分离),logging模块,配置模块,路径叠加,哈希算法

    一.os常用模块 显示当前工作目录 print(os.getcwd()) 返回上一层目录 os.chdir("..") 创建文件包 os.makedirs('python2/bin ...

  9. 字符串哈希算法(以ELFHash详解)

    更多字符串哈希算法请参考:http://blog.csdn.net/AlburtHoffman/article/details/19641123 先来了解一下何为哈希: 哈希表是根据设定的哈希函数H( ...

随机推荐

  1. RDD的Cache、Persist、Checkpoint的区别和StorageLevel存储级别划分

    为了增强容错性和高可用,避免上游RDD被重复计算的大量时间开销,Spark RDD设计了包含多种存储级别的缓存和持久化机制,主要有三个概念:Cache.Persist.Checkout. 1.存储级别 ...

  2. 递归回溯法求N皇后问题

    问题描述:在一个NN(比如44)的方格中,在每一列中放置一个皇后,要求放置的皇后不在同一行,同一列,同一斜线上,求一共有多少种放置方法,输出放置的数组. 思路解析:从(1,1)开始,一列一列的放置皇后 ...

  3. Redis学习笔记2-redis管道(pipeline)

    redis的管道(Pipelining)操作是一种异步的访问模式,一次发送多个指令,不同步等待其返回结果.这样可以取得非常好的执行效率.这就是管道,调用方法如下: 来源:http://blog.csd ...

  4. Spring Boot熟稔于心的20个常识

    1.什么是 Spring Boot? Spring Boot 是 Spring 开源组织下的子项目,是 Spring 组件一站式解决方案,主要是简化了使用 Spring 的难度,简省了繁重的配置,提供 ...

  5. 读者来信 | 如果你家HBase集群Region太多请点进来看看,这个问题你可能会遇到

    前言:<读者来信>是HBase老店开设的一个问答专栏,旨在能为更多的小伙伴解决工作中常遇到的HBase相关的问题.老店会尽力帮大家解决这些问题或帮你发出求救贴,老店希望这会是一个互帮互助的 ...

  6. C# 便捷实现可迭代对象间的赋值

    目录 都是迭代,为啥我一定要用foreach 如果换成是字典呢? 关于 foreach 都是迭代,为啥我一定要用foreach ​ 问题起源于本人的一个练手的扑克牌程序:洗完牌之后要发给场上的三人. ...

  7. NKOJ3751 扫雷游戏

    问题描述 有一款有趣的手机游戏.棋盘上有n颗地雷,玩家需要至少扫掉其中的k颗雷.每一步,玩家可以用手指在手机屏幕上划一条直线,该直线经过的地雷都会被扫除掉.问,最少需要划几次就能扫除k颗以上的地雷? ...

  8. Python学习笔记:迭代器(Iterator)详解

    一.可迭代的对象(Iterable) 1.定义:可以直接用在循环的数据类型,如list,tuple,dict,set,str,还有generator(生成器), 和带yield的函数,这些直接可以用在 ...

  9. Elasticsearch7.6学习笔记1 Getting start with Elasticsearch

    Elasticsearch7.6学习笔记1 Getting start with Elasticsearch 前言 权威指南中文只有2.x, 但现在es已经到7.6. 就安装最新的来学下. 安装 这里 ...

  10. STM32F103ZET6串口通信

    1.电平标准 根据通讯使用的电平标准不同,串口通讯可分为TTL标准和RS-232标准,如下表: 从图中可以看到,TTL电平标准使用5V表示高电平,使用0V表示低电平.在R232电平标准中,为了增加串口 ...