D. MUH and Cube Walls
 

Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one on top of the other. They defined multiple towers standing in a line as a wall. A wall can consist of towers of different heights.

Horace was the first to finish making his wall. He called his wall an elephant. The wall consists of w towers. The bears also finished making their wall but they didn't give it a name. Their wall consists of n towers. Horace looked at the bears' tower and wondered: in how many parts of the wall can he "see an elephant"? He can "see an elephant" on a segment of w contiguous towers if the heights of the towers on the segment match as a sequence the heights of the towers in Horace's wall. In order to see as many elephants as possible, Horace can raise and lower his wall. He even can lower the wall below the ground level (see the pictures to the samples for clarification).

Your task is to count the number of segments where Horace can "see an elephant".

Input

The first line contains two integers n and w (1 ≤ n, w ≤ 2·105) — the number of towers in the bears' and the elephant's walls correspondingly. The second line contains n integers ai (1 ≤ ai ≤ 109) — the heights of the towers in the bears' wall. The third line contains w integers bi (1 ≤ bi ≤ 109) — the heights of the towers in the elephant's wall.

Output

Print the number of segments in the bears' wall where Horace can "see an elephant".

Sample test(s)
input
13 5
2 4 5 5 4 3 2 2 2 3 3 2 1
3 4 4 3 2
output
2
Note

The picture to the left shows Horace's wall from the sample, the picture to the right shows the bears' wall. The segments where Horace can "see an elephant" are in gray.

#include <bits/stdc++.h>
using namespace std;
void makeNext(const int P[],int next[],int m)
{
/*
next[i]表示前i个字符中,最大前后缀相同的长度
*/
int q,k;
next[]=;
for (q=,k=;q<m;++q)
{
while(k>&&P[q]!=P[k])
k = next[k-];
/*
这里的while循环很不好理解!
就是用一个循环来求出前后缀最大公共长度;
首先比较P[q]和P[K]是否相等如果相等的话说明已经K的数值就是已匹配到的长的;
如果不相等的话,那么next[k-1]与P[q]的长度,为什么呐?因为当前长度不合适
了,不能增长模板链,就缩小看看next[k-1]
的长度能够不能和P[q]匹配,这么一直递归下去直到找到
*/
if(P[q]==P[k])//如果当前位置也能匹配上,那么长度可以+1
{
k++;
}
next[q]=k;
}
} int kmp(const int T[],const int P[],int next[],int n,int m)
{
int i,q;
makeNext(P,next,m);
int res=;
for (i=,q=;i<n;++i)
{
while(q>&&P[q]!= T[i])
q = next[q-];
/*
这里的循环就是位移之后P的前几个字符能个T模板匹配
*/
if(P[q]==T[i])
{
q++;
}
if(q==m)//如果能匹配的长度刚好是T的长度那么就是找到了一个能匹配成功的位置
{
res++;
}
}
return res;
}
int T[];
int P[];
int next[];
int n,m;
int main(){
// freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%d",&T[i]);
}
for(int i=;i<m;i++){
scanf("%d",&P[i]);
}
for(int i=;i<n-;i++){
T[i]=T[i+]-T[i];
}
n--;
for(int i=;i<m-;i++){
P[i]=P[i+]-P[i];
}
m--;
if(m==){
printf("%d\n",n+);
return ;
}
if(n==){
printf("0\n");
return ;
}
makeNext(P,next,m);
printf("%d\n",kmp(T,P,next,n,m));
return ;
}

D - MUH and Cube Walls的更多相关文章

  1. Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp

    D - MUH and Cube Walls Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  2. Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!

    D. MUH and Cube Walls 说实话,这题看懂题意后秒出思路,和顺波说了一下是KMP,后来过了一会确定了思路他开始写我中途接了个电话,回来kaungbin模板一板子上去直接A了. 题意: ...

  3. [codeforces471D]MUH and Cube Walls

    [codeforces471D]MUH and Cube Walls 试题描述 Polar bears Menshykov and Uslada from the zoo of St. Petersb ...

  4. CodeForces 471D MUH and Cube Walls -KMP

    Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of ...

  5. codeforces MUH and Cube Walls

    题意:给定两个序列a ,b, 如果在a中存在一段连续的序列使得 a[i]-b[0]==k, a[i+1]-b[1]==k.... a[i+n-1]-b[n-1]==k 就说b串在a串中出现过!最后输出 ...

  6. MUH and Cube Walls

    Codeforces Round #269 (Div. 2) D:http://codeforces.com/problemset/problem/471/D 题意:给定两个序列a ,b, 如果在a中 ...

  7. Codeforces 471 D MUH and Cube Walls

    题目大意 Description 给你一个字符集合,你从其中找出一些字符串出来. 希望你找出来的这些字符串的最长公共前缀*字符串的总个数最大化. Input 第一行给出数字N.N在[2,1000000 ...

  8. CF471D MUH and Cube Walls

    Link 一句话题意: 给两堵墙.问 \(a\) 墙中与 \(b\) 墙顶部形状相同的区间有多少个. 这生草翻译不想多说了. 我们先来转化一下问题.对于一堵墙他的向下延伸的高度,我们是不用管的. 我们 ...

  9. CodeForces–471D--MUH and Cube Walls(KMP)

    Time limit         2000 ms  Memory limit  262144 kB Polar bears Menshykov and Uslada from the zoo of ...

随机推荐

  1. Redis学习笔记之一 : 配置redis

    Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久 ...

  2. PHP buffer的机制

    PHP的buffer是这样的: 输出的字符串 => PHP buffer => 等待输出 => web 服务器的缓冲区 => tcp 缓冲区 => 客户端.过程其实相当的 ...

  3. Apache Spark 2.2.0 中文文档 - SparkR (R on Spark) | ApacheCN

    SparkR (R on Spark) 概述 SparkDataFrame 启动: SparkSession 从 RStudio 来启动 创建 SparkDataFrames 从本地的 data fr ...

  4. 【JVM命令系列】jmap

    命令基本概述 Jmap是一个可以输出所有内存中对象的工具,甚至可以将VM 中的heap,以二进制输出成文本.打印出某个java进程(使用pid)内存内的,所有'对象'的情况(如:产生那些对象,及其数量 ...

  5. Liers 树状数组+中国剩余定理

    Liers Time Limit: 14000/7000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus ...

  6. 技术领导(Technical Leader)画像

    程序员都讨厌被管理,而乐于被领导.管理的角色由PM(project manager)扮演,具体来说,PM负责提需求.改改改.大多数情况,PM是不懂技术的,这也是程序员觉得PM难以沟通的原因.而后者由技 ...

  7. Java历程-初学篇 Day08 数组

    一,什么是数组 所谓数组,是相同数据类型的元素按一定顺序排列的集合.若将有限个类型相同的变量的集合命名,那么这个名称为数组名.组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下标变量.用 ...

  8. js中 && 与 || 的妙用

    在js逻辑运算中,0."".null.false.undefined.NaN都会判为false,其他都为true(好像没有遗漏了吧,请各位确认下).这个一定要记住,不然应用||和& ...

  9. 关于python安装一些包时出现的错误解决方法

    1.关于wordcloud的安装 --win10,py3.6环境下安装总是出现安装错误,解决方法,下载wordcloud的wheel文件,进行安装. 详情参考:https://github.com/a ...

  10. EF框架搭建小总结--ModelFirst模型优先

    前言:去年刚工作的时候,也是刚刚正式接触.net,当时了解了EF以及三种开发模式,Database First.Model First .Code First.公司用的开发模式是Database Fi ...