CodeForces - 471D MUH and Cube Walls 【KMP】

D. MUH and Cube Walls
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

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.



思路:

存的时候直接存相邻元素的差,然后直接KMP就行了。不管起始位置的高度是多少,下一个元素与它的差是固定的。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
#define MAXN 200005
int n,m,a[MAXN],b[MAXN];
int T[MAXN],P[MAXN],f[MAXN];
void getFail()
{
    int len=m;
    f[0]=f[1]=0;
    for(int i=1;i<len;i++)
    {
        //如果i点匹配失败,寻找对应的匹配点
        int j=f[i];
        //如果j点在上一个匹配点依然失败,则继续向上寻找当前点的匹配点,直到匹配或达到起始点。
        while(j && P[j]!=P[i]) j=f[j];
        //更新下一个失配点,如果本失配点找到了匹配点,则下一个失配值需要更新到匹配点+1;如果没有找到匹配点,则更新到起点。
        f[i+1] = P[j]==P[i]? j+1:0;
    }
}
int find()
{
    int j=0,cnt=0;
    for(int i=0;i<n;i++)
    {
        //如果T[i]匹配失败,则向上寻找前面的匹配点
        while(j && P[j]!=T[i]) j=f[j];
        //如果找到了一个匹配点,则将j后移一位(即继续匹配下一位)
        if(P[j]==T[i]) j++;
        
        if(j==m) cnt++,j=f[j];
    }
    return cnt;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<m;i++)
        scanf("%d",&b[i]);
    if(m==1)
    {
        printf("%d\n",n);
        exit(0);
    }
    else if(n==1)
    {
        printf("0\n");
        exit(0);
    }
    else
    {
        for(int i=0;i<n-1;i++)
            T[i]=a[i+1]-a[i];
        for(int i=0;i<m-1;i++)
            P[i]=b[i+1]-b[i];
        n--; m--;
        getFail();
        printf("%d\n",find());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u013852115/article/details/80195856