Time limit         2000 ms 
Memory limit  262144 kB

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

Example

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.

题意:

见图,给定两段参差不齐的成墙,问图中左边灰色的可以在右边匹配到多少次?

匹配时可以上下移动,即只关心每一个柱的差值

分析:

一开始想到了用KMP,可是傻B地直接暴力的。。。。果断TLE

后来看了题解,很巧妙的处理了一下:

基于一段连续区间无论怎么变他们的相对位置不会变,所以分别将两个序列相邻的元素作差,一段区间的相对位置不会变,所以正好可以用差值去匹配。

代码:

  1 #include<bits/stdc++.h>
2 const int N=2e5+7;
3 int len1,len2,a[N],b[N],f[N],p[N];
4 void kmp_pre(int *x,int m,int *next)//以下感谢kuangbin神主提供的模板。
5 {
6 int i,j;
7 j=next[0]=-1;
8 i=0;
9 while(i<m)
10 {
11 while(-1!=j&&x[i]!=x[j]) j=next[j];
12 next[++i]=++j;
13 }
14 }
15 void prekmp(int *x,int m,int *kmpnext)
16 {
17 int i,j=-1;
18 kmpnext[0]=-1;
19 i=0;
20 while(i<m)
21 {
22 while(-1!=j&&x[i]!=x[j]) j=kmpnext[j];
23 if(x[++i]==x[++j]) kmpnext[i]=kmpnext[j];
24 else kmpnext[i]=j;
25 }
26 }
27 int next[N];
28 int kmp_count(int *x,int m,int *y,int n)
29 {
30 int i,j,ans=0;
31 kmp_pre(x,m,next);
32 i=j=0;
33 while(i<n)
34 {
35 while(-1!=j&&y[i]!=x[j]) j=next[j];
36 i++,j++;
37 if(j>=m)
38 {
39 ans++;
40 j=next[j];
41 }
42 }
43 return ans;
44 }
45 int main()
46 {
47 int w,n;
48 while(~scanf("%d%d",&w,&n))
49 {
50 len1=0,len2=0;
51 for(int i=0;i<w;i++)
52 {
53 scanf("%d",&a[i]);
54 if(i) f[len1++]=a[i]-a[i-1];
55 }
56 for(int i=0;i<n;i++)
57 {
58 scanf("%d",&b[i]);
59 if(i) p[len2++]=b[i]-b[i-1];
60 }
61 if(n==1)
62 {
63 printf("%d\n",w);
64 continue;
65 }
66 int ans=kmp_count(p,len2,f,len1);
67 printf("%d\n",ans);
68 }
69 return 0;
70 }

CodeForces–471D--MUH and Cube Walls(KMP)的更多相关文章

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

  2. 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 & % ...

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

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

  4. D - MUH and Cube Walls

    D. MUH and Cube Walls   Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant ...

  5. [codeforces471D]MUH and Cube Walls

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

  6. 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串中出现过!最后输出 ...

  7. Codeforces 471 D MUH and Cube Walls

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

  8. MUH and Cube Walls

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

  9. CF471D MUH and Cube Walls

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

随机推荐

  1. AcWing池塘计数

    这个题让我们求连通块得数数量,我考虑用flood fill算法. 也就是枚举这个地图每一个点,假如符合要求就bfs与这个点联通的点,并打上标记.结束后接着枚举没有被标记并且符号要求的点... 1.== ...

  2. redis在php中实际应用-hash

    Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象. 目录: 1.批量赋值:hmset,hmget,hgetall 可用于存储一条条数据,即一个 ...

  3. Transparency Tutorial with C# - Part 3

    Download image fade demo - 4 Kb Download image fade source project- 7 Kb Download image fade images ...

  4. C# 如何判断指定文件是否正被其它程序使用

    C# 如何判断指定文件是否正被其它程序使用 起因:项目中发现在操作文件时,系统经常抛出异常,表示文件正被其它程序占用. 需求:为了事先判断,以确认指定的文件是否正被其它程序使用,需要方法进行判断. 思 ...

  5. luogu题解 P1462 【通往奥格瑞玛的道路】二分+spfa

    题目链接: https://www.luogu.org/problemnew/show/P1462 思路: 又是一道水题,很明显二分+最短路 而且这道题数据非常水,spfa有个小错误居然拿了91分还比 ...

  6. O005、远程管理 KVM 虚机

    参考https://www.cnblogs.com/CloudMan6/p/5256018.html   上一节我们通过 virt-manager 在本地主机上创建并管理 KVM 虚机,其实 virt ...

  7. Maven中setting.xml 配置详解

    文件存放位置 全局配置: ${M2_HOME}/conf/settings.xml 用户配置: ${user.home}/.m2/settings.xml note:用户配置优先于全局配置.${use ...

  8. 常用的排序算法介绍和在JAVA的实现(一)

    一.写随笔的原因:排序比较常用,借此文介绍下排序常用的算法及实现,借此来MARK一下,方便以后的复习.(本人总是忘得比较快) 二.具体的内容: 1.插入排序 插入排序:在前面已经排好序的序列中找到合适 ...

  9. python、第八篇:索引原理与慢查询优化

    一 介绍 1. 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查 ...

  10. 架构师成长之路5.1-Saltstack安装及入门

    点击架构师成长之路 架构师成长之路5.1-Saltstack安装及入门 (安装.配置.启动) 配置管理工具: Pupper:1. 采用ruby编程语言:2. 安装环境相对较复杂:3.不支持远程执行,需 ...