博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 471D. MUH and Cube Walls(差分 + KMP,93 ms)
阅读量:706 次
发布时间:2019-03-21

本文共 3365 字,大约阅读时间需要 11 分钟。

一、题目描述

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·10^5) — the number of towers in the bears’ and the elephant’s walls correspondingly. The second line contains n integers ai (1 ≤ ai ≤ 10^9) — the heights of the towers in the bears’ wall. The third line contains w integers bi (1 ≤ bi ≤ 10^9) — 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.

在这里插入图片描述

二、算法分析说明

由图示,所求答案为:不同的木立方体堆起来的塔连成的墙的顶端具有指定的边缘的段数。

可以看出对与要匹配的部分相同的任何一段,当这一段的塔高于组成要比对的部分的的高的变化一致时,算作一次匹配。
计算出输入的数组 a、b 的差分数组 da、db,然后套 KMP 的模板即可。
注意当匹配的长度为 1 时,在任何一个位置都可以匹配,答案为 n。

三、AC 代码(93 ms)

#include
#pragma warning(disable:4996)int n, w, a[200002], b[200002], da[200002], db[200002], next[200002];template
inline void getnext(const _PTy* const _Pattern, _NTy* const _Next, const size_t& _PatternLen) {
size_t i = 1; _NTy j = 0; _Next[1] = 0; while (i <= _PatternLen) {
if (j == 0 || _Pattern[i] == _Pattern[j]) {
++i, ++j, _Next[i] = j; } else j = _Next[j]; }}//The index of the head of _Pattern string is 1.template
inline _RTy KMP(const _TTy* const _Target, const _PTy* const _Pattern, _NTy* const _Next, const size_t& _TargetLen, const size_t& _PatternLen) {
_RTy c = 0; size_t i = 1; _NTy j = 1; _Next[1] = 0, getnext(_Pattern, _Next, _PatternLen); while (i <= _TargetLen) {
if (j == 0 || _Target[i] == _Pattern[j]) {
++i, ++j; } else j = _Next[j]; if (j > _PatternLen) {
++c, j = _Next[j]; } } return c;}//The index of the head of _Pattern string is 1.int main() {
scanf("%d%d", &n, &w); if (w == 1) {
printf("%d\n", n); return 0; } for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]); da[i] = a[i] - a[i - 1]; } for (int i = 1; i <= w; ++i) {
scanf("%d", &b[i]); db[i] = b[i] - b[i - 1]; } printf("%u\n", KMP(da + 1, db + 1, next, n - 1, w - 1)); return 0;}

转载地址:http://umtez.baihongyu.com/

你可能感兴趣的文章