471D - MUH and Cube Walls (KMP)

来源:互联网 发布:宠物商城源码 编辑:程序博客网 时间:2024/06/07 16:50

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
13 52 4 5 5 4 3 2 2 2 3 3 2 13 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.

 此方法字符数组和数组求子串的个数是一样的
#include<cstdio>#include<math.h>#include<cstring>#include<iostream>#include<algorithm>#include<map>#include<stdlib.h>#define fo(i,a,b) for(int i=a;i<=b;i++)#define fd(i,a,b) for(int i=a;i>=b;i--)#define maxn 300005#define ll long long#define mem(a,b) memset(a,b,sizeof(a))const int mod=1e9+7;using namespace std;int a[maxn],b[maxn],f[maxn];int s1[maxn],s2[maxn]; int n,w;void getnexta(){memset(f,0,sizeof(f));int k = -1,j = 0;f[0] = -1;while(j < w){if(k == -1||s2[k] == s2[j]){f[j + 1] = k + 1;j ++;k ++;}else{k = f[k];}}}int kmp(){int ans= 0;getnexta();int i = 0,j = 0;while(i < n && j < w){if(j == -1 || s1[i] == s2[j]){i ++;j ++;}else{ j = f[j];}if(j == w){ans ++;j = f[j];}}return ans;}int main(){    while(~scanf("%d%d",&n,&w))//从左减右    {        for(int i=0; i<n; i++)            scanf("%d",&a[i]);        for(int i=0; i<w; i++)            scanf("%d",&b[i]);        if(w==1)        {            printf("%d\n",n);        }        else        {            w--,n--;            for(int i=0; i<n; i++)            {                s1[i]=(a[i]-a[i+1]);            }            for(int i=0; i<w; i++)            {                s2[i]=(b[i]-b[i+1]);            }            printf("%d\n",kmp());        }    }    return 0;}
这种next的数组定义就和字符数组不一样,上面的那个符合
4 2
2 2 2 2
2 2
输出的是 3
下面的这个却是 2 
#include<cstdio>#include<cstring>#include<algorithm>#include <iostream>#include <string>const int maxn=300005;using namespace std;int f[maxn];int a[maxn],s[maxn], c[maxn],d[maxn];int n,w;void getfill(){    memset(f,0,sizeof(f));  //根据其前一个字母得到    for(int i=1; i<w; i++)    {        int j=f[i];        while(j && s[i]!=s[j])            j=f[j];        f[i+1]=(s[i]==s[j])?j+1:0;    }}int kmp(){    int ans=0;    getfill();    int j=0;    for(int i=0; i<n; i++)    {        while(j && a[i]!=s[j])            j=f[j];        if(a[i]==s[j])            j++;        if(j==w)        {            ans++;        }    }    return ans;}int main(){    while(~scanf("%d%d",&n,&w))    {        for(int i=0; i<n; i++)            scanf("%d",&c[i]);        for(int i=0; i<w; i++)            scanf("%d",&d[i]);        if(w==1)            printf("%d\n",n);        else        {            n--;w--;            for(int i=0; i<n; i++)            {                a[i]=c[i]-c[i+1];            }            for(int i=0; i<w; i++)            {                s[i]=d[i]-d[i+1];            }            printf("%d\n",kmp());        }    }    return 0;}


0 0