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. border-radius:50%和100%究竟有什么区别

    之前写css圆形时总是直接设置border-radius为50%.后来看某css动画网站时发现作者都是用的100%.遂去了解了一下2者的差别. border-radius的值是百分比的话,就相当于盒子 ...

  2. NOIP 11.01 应试记录

    快排代码 void quicksort(int left,int right) { int u,j,t,temp; if(letf>right) return; temp=a[left]; i= ...

  3. Android ListView getViewTypeCount 的返回值问题解决

    最近在学慕课网上的一个实战课程,期间有一个智能聊天机器人模块. 聊天界面通过 ListView 显示,用 Adapter 加载.一般来说,单对单的聊天,两者发出的话分别列在聊天页面的左右两边.所以,在 ...

  4. Python自学笔记-filter()函数(来自廖雪峰的官网Python3)

    感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. Python内 ...

  5. c++ 11 移动语义、std::move 左值、右值、将亡值、纯右值、右值引用

    为什么要用移动语义 先看看下面的代码 // rvalue_reference.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #includ ...

  6. Sqlserver数据库 通过表触发器 实时通知应用程序

    /* Sqlserver数据库开始相关服务 以下示例显示了如何查看 OLE Automation Procedures 的当前设置.0未启用 */ EXEC sp_configure 'show ad ...

  7. [Python]Codecombat攻略之远边的森林Forest(1-40关)

    首页:https://cn.codecombat.com/play语言:Python 第二界面:远边的森林Forest(40关)时间:2-6小时内容:if/else.关系操作符.对象属性.处理输入网页 ...

  8. 史上最难的一道Java面试题 (分析篇)

    博客园 匠心零度 转载请注明原创出处,谢谢! 无意中了解到如下题目,觉得蛮好. 题目如下: public class TestSync2 implements Runnable { int b = 1 ...

  9. javaScript基础的基础

    JavaScript是一个脚本语言,需要有宿主文件,他的宿主文件是HTML文件. 与JAVA没有直接关系 一般写在 1.head里面 2.body里面 3.</html>后面 一般写在&l ...

  10. Delphi实现电脑端微信图片文件解密

    电脑端微信收到图片后是存在了“C:\Users\系统用户名\Documents\WeChat Files\微信帐号\Data”目录下的,但文件不能直接使用图片浏览器打开的,因为做了一些加密,之前有个朋 ...