D. MUH and Cube Walls

https://codeforces.com/problemset/problem/471/D

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

Examples

input

Copy

13 5
2 4 5 5 4 3 2 2 2 3 3 2 1
3 4 4 3 2

output

Copy

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.


题意:第二个给出的图片可以高度任意移动,问在第一个方块中可以有多少长度一样并且高度轮廓相同的数量。

思路:开始想枚举高度,发现这样肯定是T的。比较巧妙的是不管高度怎么变,同一个图形里的相邻高度差是不变的。

所以把两个图形的差分求出来,然后对两个差分进行kmp匹配。

代码.kmp的时候下标从0开始比较正确

当然这题也可以用字符串哈希去做。预处理出差分的哈希,然后在一个循环中去找是不是对应的值是不是一样的。卡ull的话换成多质数或者多模数就好了

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+199;
typedef long long LL;
int nxt[maxn];
int a[maxn],b[maxn];
int p[maxn+10];//模式 
int s[maxn+10];//文本 
int n,w;
void build(int s[],int n)//构造nxt数组相当于把匹配串错开一位自己进行比较
{
     int k=0;nxt[0]=0;
     for(int i=1;i<n;i++)
     {
          while(k&&s[i]!=s[k])//注意是while循环,因为可能回退一次之后依旧不相等
               k=nxt[k-1];
          if(s[i]==s[k])
               k++;
          nxt[i]=k;
     }
}
int kmp(int w[],int s[],int lw,int ls)
{
	 int ans=0;
     build(s,ls);
     int j=0;
     for(int i=0;i<lw;i++)
     {
          while(j&&w[i]!=s[j])//不相等就回退
               j=nxt[j-1];
          if(w[i]==s[j])//相等就++
               j++;
          if(j==ls)
          {
          	ans++;j=nxt[j-1];//匹配串已经匹配完了bingo
		  }              
     }
return ans;
}
//用kmp的时候下标还是从0开始把 
int main(void)
{
  cin>>n>>w;
  for(int i=0;i<n;i++) cin>>a[i];
  for(int i=0;i<w;i++) cin>>b[i];
  if(w==1){
  	cout<<n<<endl;return 0;
  }
  for(int i=0;i<n-1;i++) s[i]=a[i+1]-a[i];
  for(int i=0;i<w-1;i++) p[i]=b[i+1]-b[i];
//  for(int i=0;i<n-1;i++) cout<<s[i]<<" ";
//  cout<<endl;
//  for(int i=0;i<w-1;i++) cout<<p[i]<<" ";
//  cout<<endl;
   --n;--w;
  cout<<kmp(s,p,n,w)<<endl;
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/108406853