题目:
Description
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".
Sample Input
Input 13 5 2 4 5 5 4 3 2 2 2 3 3 2 1 3 4 4 3 2 Output 2Hint
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.
首先把图形转换成数字化的定义:
输入2个数组a和b,在a里面找连续的一段,使得和b一样长,而且所有对应的2个数的差都是一样的。
输出也多少个满足条件的段(即位置)
比如输入
13 5 2 4 5 5 4 3 2 2 2 3 3 2 1 3 4 4 3 2 取a的4 5 5 4 3这一段,那么它和b的差都是1,取2 3 3 2 1这一段,那么差都是-1
然后,理解了题意,就要开始思考了。
规律还是很明显的,上面的表述可以进一步转化:
2个等长的数组,满足a[i]-a[i-1]==b[i]-b[i-1]恒成立。
于是,在最开始输入的时候,只需要记录差分即可。
第1个数的值不用记录,所以我们实际上得到了n-1长的数组a和w-1长的数组b,
要求的就是问a有多少个位置可以匹配b。
这不就是KMP了吗。
不过,稍微注意一下,当w=1或者n=1的时候需要特殊处理。
代码:
#include<iostream> using namespace std; int a[200001]; int b[200001]; int nextb[200001]; void getnext(int *t, int m, int *next) { int i = 1, j = 0; next[1] = 0; while (i < m) { if (j == 0 || t[i] == t[j])next[++i] = ++j; else j = next[j]; } } int main() { int n, w, x, y; scanf("%d%d%d", &n, &w, &x); for (int i = 1; i < n; i++) { scanf("%d", &y); a[i] = y - x; x = y; } scanf("%d", &x); for (int i = 1; i < w; i++) { scanf("%d", &y); b[i] = y - x; x = y; } int sum = 0; if (n == 1)sum += (w == 1); else if (w == 1)sum += n; else if (n >= w) { getnext(b, w, nextb); int i = 1, j = 1; while (i < n) { if (j == 0) { j++; i++; continue; } if (n - i < w - j)break; if (a[i] == b[j]) { i++; j++; if (j >= w) { sum++; j = nextb[w - 1]; i--; } } else { if (nextb[j])j = nextb[j]; else { i++; j = 1; } } } } cout << sum << endl; return 0; }
