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

时间:2021-09-04 15:32:08
D - MUH and Cube Walls

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 Input

Input
13 5
2 4 5 5 4 3 2 2 2 3 3 2 1
3 4 4 3 2
Output
2

Hint

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.

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

第一个kmp。。。。

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<string> #define N 200005
#define M 15
#define mod 10000007
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n;
int w;
int a[N],b[N];
int c[N],d[N];
int next[N];
int ans; void get_next()
{
int i = , j = ;
next[] = ;
while(i <=w-)
{
if(j == || d[i] == d[j])
{
j++;
i++;
next[i] = j;
}
else
j = next[j];
}
// for(i=1;i<=w-1;i++){
// printf(" i=%d d=%d next=%d\n",i,d[i],next[i]);
// }
} void ini()
{
int i;
ans=;
for(i=;i<=n;i++){
scanf("%d",&a[i]);
}
for(i=;i<=n-;i++){
c[i]=a[i+]-a[i];
}
// for(i=1;i<=n-1;i++){
// printf(" %d",c[i]);
// }
for(i=;i<=w;i++){
scanf("%d",&b[i]);
}
for(i=;i<=w-;i++){
d[i]=b[i+]-b[i];
}
} void solve()
{
int i,j;
// printf(" 1221\n");
if(w==){
ans=n;
return ;
}
i=,j=;
while(i<=n-){
// printf(" i=%d j=%d ans=%d\n",i,j,ans);
if(j== || c[i]==d[j]){
if(j==w-){
ans++;
//i++;
j=next[j];
}
else{
i++;
j++;
}
}
else{
j=next[j];
}
}
} void out()
{
printf("%d\n",ans);
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
// for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
while(scanf("%d%d",&n,&w)!=EOF)
{
// if(n==0 && m==0) break;
//printf("Case %d: ",ccnt);
ini();
get_next();
solve();
out();
} return ;
}