Codeforces 471D. MUH and Cube Walls(差分 + KMP,93 ms)

这篇博客介绍了Codeforces 471D题目的解决方案,即计算在熊的立方体墙中可以看到多少段与大象墙高度匹配的序列。通过计算差分数组和应用KMP算法,可以找出匹配的段数。文章包含题目描述、算法分析和93ms的AC代码。
摘要由CSDN通过智能技术生成

一、题目描述

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

在这里插入图片描述

二、算法分析说明

由图示,所求答案为:不同的木立方体堆起来的塔连成的墙的顶端具有指定的边缘的段数。
可以看出对与要匹配的部分相同的任何一段,当这一段的塔高于组成要比对的部分的的高的变化一致时,算作一次匹配。
计算出输入的数组 a、b 的差分数组 da、db,然后套 KMP 的模板即可。
注意当匹配的长度为 1 时,在任何一个位置都可以匹配,答案为 n。

三、AC 代码(93 ms)

#include<cstdio>
#pragma warning(disable:4996)
int n, w, a[200002], b[200002], da[200002], db[200002], next[200002];
template<class _PTy, class _NTy> 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<class _TTy, class _PTy, class _NTy, class _RTy = unsigned> 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值