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. python读取.mat文件

    可以先看一下.mat中存了些什么: import scipy.io as sio box_file = '/home/bnrc/formatm/test/1479504458876408533_box ...

  2. VC-基础:VC++动态链接库(DLL)编程深入浅出

    1.概论 先来阐述一下DLL(Dynamic Linkable Library)的概念,你可以简单的把DLL看成一种仓库,它提供给你一些可以直接拿来用的变量.函数或类.在仓库的发展史上经历了“无库-静 ...

  3. House of force

    0x00 利用要点 1.申请一块非常大的块. 2.精心构造size覆盖top chunk的chunk header. 3.调用malloc()实现任意地址写 0x01 申请一块非常大的块. 申请一个负 ...

  4. SniperOJ-leak-x86-64

    参考:1.借助DynELF实现无libc的漏洞利用小结 2.一步一步学ROP之linux_x64篇 - 蒸米 题目源码 #include <stdio.h> #include <un ...

  5. jquery 获得某一组name的id并合并

    var attachmentids = $("input[name='attachmentid']").map(function(){return $(this).val()}). ...

  6. mysqldump 备份导出数据排除某张表或多张表

    可以使用--ignore-table=dbname.tablename 忽略一张表 /usr/bin/mysqldump --set-gtid-purged=OFF -h127.0.0.1 -uroo ...

  7. 剑指Offer(书):二叉树的镜像

    题目:操作给定的二叉树,将其变换为源二叉树的镜像. public void Mirror(TreeNode root) { if (root == null) { return ; } if (roo ...

  8. django基础(web框架,http协议,django安装)

    学习Django之前我们先来看什么是OSI七层模型: 应用层 表示层       应用层(五层模型中把这三层合成一个应用层) http协议 会话层 传输层                  提供端口对 ...

  9. Hibernate 框架理解

    Hibernate框架简化了java应用程序与数据库交互的开发.Hibernate是一个开源,轻量级的ORM(对象关系映射)工具. ORM工具简化浏览数据的创建,数据处理和数据访问.它是将对象映射到数 ...

  10. [转]构建Python+Selenium2自动化测试环境(二)

    构建Python+Selenium2自动化测试环境完成之后,就需要测试支持python的selenium的版本是否都支持在不同浏览器上运行,当前我们分别在三个最通用的浏览器上通过脚本来测试. 1.在I ...