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. python数据结构之树(概述)

    树 在计算机科学中,树是分层结构的抽象模型 .本篇学习笔记记录树的内容如下: 树的基本功能:定义.术语.ADT 树的遍历方法:前序.中序.后序 树的定义 第一种:树由一组节点和一组连接节点的边组成.树 ...

  2. Sorted sets

    Redis in Action JOSIAH L. CARLSON MANNING Shelter Island ZSETs offer the ability to store a mapping ...

  3. Excel-字符串连接

    使用函数concatenate()将多个字符连接起来

  4. web window pixel等笔记

    原文:http://www.w3cplus.com/css/viewports.html 屏幕尺寸 Screen size =显示器尺寸 screen.width 和 screen.height.这两 ...

  5. sass,less的安装及sass的教程

    装scss(window) 首相安装ruby http://www.sasschina.com/install/ scss转译css http://www.cnblogs.com/52css/arch ...

  6. tornado : 异步、非阻塞

    The terms asynchronous and non-blocking are closely related and are often used interchangeably, but ...

  7. iOS10网络权限数据

    参考地址:1.http://www.cocoachina.com/ios/20180723/24274.html   https://blog.csdn.net/wang_bo_justone/art ...

  8. MySQL字符集的一个坑

    MySQL字符集的一个坑 http://imysql.com/2013/10/29/misunderstand-about-charset-handshake.shtml MySQL字符集的一个坑 1 ...

  9. [LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  10. [LeetCode] 595. Big Countries_Easy tag: SQL

    There is a table World +-----------------+------------+------------+--------------+---------------+ ...