D. MUH and Cube Walls
Polar Bears Menshykov and Uslada from the zoo of St. Petersburg and Elephant Horace from the zoo's Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one in 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 He 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 what 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 MATC h as a sequence the heights of the towers in Horace ' s wall. In order to see as many elephants as possible, the 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 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 a i (1≤ a i ≤109)-the heights of the towers in the Bears ' wall. The third line Contains w integers b i (1≤ b i ≤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" is in gray.
Test Instructions : give you two sequences in a two-dimensional coordinate plan
Exactly the b sequence can be nested in the scheme number below the a sequence,
The KMP, that is, the number of j==m, but we can preprocess the array into a difference
///1085422276#include <bits/stdc++.h>using namespacestd; typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))inline ll read () {ll x=0, f=1; CharCh=GetChar (); while(ch<'0'|| Ch>'9') { if(ch=='-') f=-1; CH=GetChar (); } while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; CH=GetChar (); } returnx*F;}//****************************************#defineMAXN 200000+5#defineMoD 1000000007intp[maxn],a[maxn],b[maxn],n,m;intMain () {n=read (); M=read (); for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); } for(intI=2; i<=n;i++) a[i-1]=a[i]-a[i-1];n--; for(intj=1; j<=m;j++) {scanf ("%d",&B[j]); } for(intI=2; i<=m;i++) b[i-1]=b[i]-b[i-1];m--; if(m==0) {cout<<n+1<<endl;return 0; } if(n==0) {cout<<0<<endl;return 0; } memset (P,0,sizeof(p)); intj=0; for(intI=2; i<=m;i++) { while(j>0&&b[j+1]!=b[i]) j=P[j]; if(b[j+1]==b[i]) J + +; P[i]=J; } intans=0; J=0; for(intI=1; i<=n;i++) { while(j>0&&b[j+1]!=a[i]) j=P[j]; if(b[j+1]==a[i]) J + +; if(j==m) {ans++; }} cout<<ans<<Endl; return 0;}
Code
Codeforces Round #269 (Div. 2) D. MUH and Cube Walls KMP