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.


  题目大意:给定一个序列A,它的某一个子串通过每个元素加上一个x,使得这个子串和序列B相同,问有多少个这样的开始位置不同的子串。

  如果没有加上x这个条件就是KMP模板,那么只能找个不会变化的量来进行KMP,这个量就是后一项和前一项的差(没有后一项的的话不管)。特殊地,当m = 1时,结果为n。

Code

 /**
* codeforces
* Problem#471D
* Accepted
* Time:46ms
* Memory:3720k
*/
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<ctime>
#include<set>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef bool boolean;
#define INF 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#ifndef WIN32
#define AUTO "%lld"
#else
#define AUTO "%I64d"
#endif
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-');
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} int n, m;
int *S, *T;
int *f; inline void init() {
readInteger(n);
readInteger(m);
S = new int[(const int)n];
T = new int[(const int)m];
int last = ;
for(int i = , a; i <= n; i++) {
readInteger(a);
if(i > ) {
S[i - ] = a - last;
}
last = a;
}
for(int i = , a; i <= m; i++) {
readInteger(a);
if(i > ) {
T[i - ] = a - last;
}
last = a;
}
n--, m--;
} inline void getFail() {
f = new int[(const int)(m + )];
f[] = f[] = ;
for(int i = , j; i < m; i++) {
j = f[i];
while(j > && T[i] != T[j]) j = f[j];
f[i + ] = (T[i] == T[j]) ? (j + ) : ();
}
} int res = ;
inline void kmp() {
getFail();
for(int i = , j = ; i < n; i++) {
while(j > && S[i] != T[j]) j = f[j];
if(S[i] == T[j] && j < m) j++;
if(j == m) res++, j = f[j];
}
} inline void solve() {
if(m == ) {
printf("%d", n + );
return;
}
kmp();
printf("%d", res);
} int main() {
init();
solve();
return ;
}

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

  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. D - MUH and Cube Walls

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

  4. [codeforces471D]MUH and Cube Walls

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

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

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

  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. 不移除通知的话,出现的bug

    没销毁的私聊控制器仍然监听到了通知,发送表情

  2. 数据库之char vchar nchar nvchar的区别

    转自:http://blog.csdn.net/a11112244444/article/details/51475107 首先介绍一下定长或变长 所谓定长就是长度固定的,当输入的数据长度没有达到指定 ...

  3. mybatis联接查询例子

    where判断如果放在最外层就是对连接查询后的结果经行筛选. SELECT * from ( and lw_area.area_id like '35%' ) la LEFT JOIN ( selec ...

  4. Steady Cow Assignment---poj3189(多重匹配+二分)

    题目链接:http://poj.org/problem?id=3189 题意:有n头牛,B个牛棚,每头牛对牛棚都有一个喜欢度,接下来输入N*B的矩阵第i行第j列的数x表示:第i头牛第j喜欢的是x; 第 ...

  5. HTTP 协议详解(转载)

    原文: http://kb.cnblogs.com/page/130970/#httpmeessagestructe HTTP协议详解 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, ...

  6. qt——for循环里创建widget

    在for循环里创建 widget,比如test类 不能使用 test t; 而要使用 test t = new test(): for (i=0;i<=3;i++) { QPushButton* ...

  7. 06_常用 Linux 命令的基本使用

    常用 Linux 命令的基本使用 目标 理解学习 Linux 终端命令的原因 常用 Linux 命令体验 01. 学习 Linux 终端命令的原因 Linux 刚面世时并没有图形界面,所有的操作全靠命 ...

  8. SQL Server学习路径(文章目录)

    SQL Server文章目录 SQL Server文章目录(学习路径)  转自:http://www.cnblogs.com/CareySon/archive/2012/05/08/2489748.h ...

  9. 群用户通过微信小程序可以更好地协作了

    今天,小程序向开发者开放了群ID的接口能力.简单地说,就是当你把小程序分享在群聊中,被点击后开发者可获取群ID和群名称,也方便更好地针对群场景提供个性化服务.不同的群有各自的群ID,那么这个新能力开发 ...

  10. 001-nginx基础配置-location

    一.基础语法 Location block 的基本语法形式是: location [=|~|~*|^~|@] pattern { ... } [=|~|~*|^~|@] 被称作 location mo ...