链接:戳这里
D. MUH and Cube Walls
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
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" are in gray.
题意:
给出n座塔的高度ai,以及m座塔的高度bi。
可以任意多次同时,升高或者下降所有的n座塔。
找出有多少次可以使得,当前下降或升高完之后n座塔出现一段连续的高度与bi完全匹配
思路:
由于可以任意同时上升或者下降一定的高度去匹配,所以每个塔都可以作为开始匹配的点
发现n座塔的相对高度是不变的,不管怎么调整,都是唯一的。
然后这些相对的高度,去匹配m座塔bi,找出有多少可以匹配的连续塔。嗯 这就是kmp了
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n,m;
ll a[200100],b[200100];
int f[200100];
void getfail(){
int j=0;
f[0]=f[1]=0;
for(int i=1;i<m;i++){
j=f[i];
while(j && b[j]!=b[i]) j=f[j];
if(b[i]==b[j]) f[i+1]=j+1;
else f[j+1]=0;
}
}
void KMP(){
n--;m--;
getfail();
int ans=0;
int j=0;
for(int i=0;i<n;i++){
while(j && b[j]!=a[i]) j=f[j];
if(a[i]==b[j]) j++;
if(j==m) ans++;
}
printf("%d\n",ans);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++) scanf("%I64d",&a[i]);
for(int i=0;i<m;i++) scanf("%I64d",&b[i]);
if(m>n) printf("0\n");
else {
if(m==1) printf("%d\n",n);
else {
for(int i=1;i<n;i++) a[i-1]=a[i]-a[i-1];
for(int i=1;i<m;i++) b[i-1]=b[i]-b[i-1];
KMP();
}
}
return 0;
}