D - MUH and Cube Walls

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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
2

Hint

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.

第一个kmp。。。。

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<string> #define N 200005
#define M 15
#define mod 10000007
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n;
int w;
int a[N],b[N];
int c[N],d[N];
int next[N];
int ans; void get_next()
{
int i = , j = ;
next[] = ;
while(i <=w-)
{
if(j == || d[i] == d[j])
{
j++;
i++;
next[i] = j;
}
else
j = next[j];
}
// for(i=1;i<=w-1;i++){
// printf(" i=%d d=%d next=%d\n",i,d[i],next[i]);
// }
} void ini()
{
int i;
ans=;
for(i=;i<=n;i++){
scanf("%d",&a[i]);
}
for(i=;i<=n-;i++){
c[i]=a[i+]-a[i];
}
// for(i=1;i<=n-1;i++){
// printf(" %d",c[i]);
// }
for(i=;i<=w;i++){
scanf("%d",&b[i]);
}
for(i=;i<=w-;i++){
d[i]=b[i+]-b[i];
}
} void solve()
{
int i,j;
// printf(" 1221\n");
if(w==){
ans=n;
return ;
}
i=,j=;
while(i<=n-){
// printf(" i=%d j=%d ans=%d\n",i,j,ans);
if(j== || c[i]==d[j]){
if(j==w-){
ans++;
//i++;
j=next[j];
}
else{
i++;
j++;
}
}
else{
j=next[j];
}
}
} void out()
{
printf("%d\n",ans);
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
// for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
while(scanf("%d%d",&n,&w)!=EOF)
{
// if(n==0 && m==0) break;
//printf("Case %d: ",ccnt);
ini();
get_next();
solve();
out();
} return ;
}

Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp的更多相关文章

  1. Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!

    D. MUH and Cube Walls 说实话,这题看懂题意后秒出思路,和顺波说了一下是KMP,后来过了一会确定了思路他开始写我中途接了个电话,回来kaungbin模板一板子上去直接A了. 题意: ...

  2. Codeforces Round #269 (Div. 2) B. MUH and Important Things

    It's time polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from t ...

  3. Codeforces Round #269 (Div. 2) A B C

    先说C 题目链接:http://codeforces.com/problemset/problem/471/C 题目意思:有 n 张卡,问能做成多少种不同楼层(floor)的 house.注意这 n ...

  4. Codeforces Round #269 (Div. 2) A,B,C,D

    CodeForces - 471A 首先要有四个数相等,然后剩下两个数不同就是Bear,否则就是Elephant. #include <bits/stdc++.h> using names ...

  5. Codeforces Round #269 (Div. 2)

    A 题意:给出6根木棍,如果有4根相同,2根不同,则构成“bear”,如果剩余两个相同,则构成“elephant” 用一个数组分别储存各个数字出现的次数,再判断即可 注意hash[i]==5的时候,也 ...

  6. CodeForces 471D MUH and Cube Walls -KMP

    Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of ...

  7. MUH and Cube Walls

    Codeforces Round #269 (Div. 2) D:http://codeforces.com/problemset/problem/471/D 题意:给定两个序列a ,b, 如果在a中 ...

  8. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. Codeforces Round #271 (Div. 2)-B. Worms

    http://codeforces.com/problemset/problem/474/B B. Worms time limit per test 1 second memory limit pe ...

  2. 多线程下载命令--axel

    axel是轻量级多线程下载工具,对比传统下载工具curl和wget,下载速度上提升不少 centos安装如下 yum install -y axel 命令行用法 axel [option] url1 ...

  3. HDU-2544-最短路(Bellman-Ford)

    Bellman-Ford算法是一个时间复杂度很高,但是它可以用来判断负环 负环就是上面的图,那个环的整体值小于零了,所以就是负环. 我们用Bellman-Ford算法进行更新,打一个表出来: k a ...

  4. 初涉倍增&&LCA【在更】

    一种特殊的枚举算法 什么是倍增 顾名思义,即每一次翻倍增加.那么,这样我们就有了一种$O(logn)$阶的方法处理枚举方面的问题了. 参考:[白话系列]倍增算法 一些题目 [倍增]luoguP1613 ...

  5. (18)zabbix值映射Value mapping

    1. 介绍 zabbix为了显示更人性化的数据,在使用过程中,我们可以将获取到得数据映射为一个字符串. 比如,我们写脚本监控MySQL是否在运行中, 一般返回0表示数据库挂了,1表示数据库正常,还有各 ...

  6. linux系统产生随机数的6种方法

    linux系统产生随机数的6种方法 方法一:通过系统环境变量($RANDOM)实现: [root@test ~]# echo $RANDOM 11595 [root@test ~]# echo $RA ...

  7. Python基础:列表(list)和元组(tuple)

    学一门语言,可以用对比其他语言方法加深对这门语言特点的理解. 一.定义:列表和元组,都是一个可以放置任意数据类型的有序集合. mutable的列表:动态的,可以改变元素 immutable的元组:静态 ...

  8. js php 互调

    1 JS方式调用PHP文件并取得php中的值 举一个简单的例子来说明: 如在页面a.html中用下面这句调用: <! DOCTYPE HTML><html><head&g ...

  9. magic mouse 2 在Mac上灵敏度太低的解决办法

    1.打开终端 2.输入以下代码查看当前鼠标移动速度 defaults read -g com.apple.mouse.scaling 你会看到输出的是 “3”,这是初始速度 3.输入以下代码改变鼠标移 ...

  10. undertow的PUT参数获取问题

    今天使用undertow遇到一个问题,记录一下: 首先,maven配置如下: <dependency> <groupId>org.springframework.boot< ...