[codeforces471D]MUH and Cube Walls

试题描述

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

输入

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.

输出

Print the number of segments in the bears' wall where Horace can "see an elephant".

输入示例


输出示例


数据规模及约定

见“输入

题解

两个序列差分一下后跑 KMP。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 200010
int n, m, S[maxn], T[maxn], Fail[maxn]; int main() {
n = read(); m = read();
for(int i = 1; i <= n; i++) S[i] = read();
for(int i = 1; i <= m; i++) T[i] = read(); if(m == 1) return printf("%d\n", n), 0; for(int i = 1; i < n; i++) S[i] = S[i+1] - S[i]; n--;
for(int i = 1; i < m; i++) T[i] = T[i+1] - T[i]; m--;
for(int i = 2; i <= m + 1; i++) {
int j = Fail[i-1];
while(j > 1 && T[j] != T[i-1]) j = Fail[j];
Fail[i] = T[j] == T[i-1] ? j + 1 : 1;
}
int p = 1, ans = 0;
for(int i = 1; i <= n; i++) {
while(p > 1 && T[p] != S[i]) p = Fail[p];
if(T[p] == S[i] && p == m) ans++;
p = T[p] == S[i] ? p + 1 : 1;
} printf("%d\n", ans); return 0;
}

[codeforces471D]MUH and Cube Walls的更多相关文章

  1. D - MUH and Cube Walls

    D. MUH and Cube Walls   Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant ...

  2. Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp

    D - MUH and Cube Walls Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

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

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

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

  5. codeforces MUH and Cube Walls

    题意:给定两个序列a ,b, 如果在a中存在一段连续的序列使得 a[i]-b[0]==k, a[i+1]-b[1]==k.... a[i+n-1]-b[n-1]==k 就说b串在a串中出现过!最后输出 ...

  6. MUH and Cube Walls

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

  7. Codeforces 471 D MUH and Cube Walls

    题目大意 Description 给你一个字符集合,你从其中找出一些字符串出来. 希望你找出来的这些字符串的最长公共前缀*字符串的总个数最大化. Input 第一行给出数字N.N在[2,1000000 ...

  8. CF471D MUH and Cube Walls

    Link 一句话题意: 给两堵墙.问 \(a\) 墙中与 \(b\) 墙顶部形状相同的区间有多少个. 这生草翻译不想多说了. 我们先来转化一下问题.对于一堵墙他的向下延伸的高度,我们是不用管的. 我们 ...

  9. CodeForces–471D--MUH and Cube Walls(KMP)

    Time limit         2000 ms  Memory limit  262144 kB Polar bears Menshykov and Uslada from the zoo of ...

随机推荐

  1. Base64编码与解码 分类: 中文信息处理 2014-11-03 21:58 505人阅读 评论(0) 收藏

    Base64是一种将二进制转为可打印字符的编码方法,主要用于邮件传输.Base64将64个字符(A-Z,a-z,0-9,+,/)作为基本字符集,把所有符号转换为这个字符集中的字符. 编码: 编码每次将 ...

  2. B/S和C/S示意图

    B/S C/S

  3. 数据库执行计划慢导致I/O 慢

    Memory Statistics~~~~~~~~~~~~~~~~~ Begin End ------------ ------------ Host Mem (MB): 16,338.5 16,33 ...

  4. jmeter(十七)逻辑控制器

    JMeter中的Logic Controller用于为Test Plan中的节点添加逻辑控制器. JMeter中的Logic Controller分为两类:一类用来控制Test Plan执行过程中节点 ...

  5. Android开发学习——android反编译

    第一种: 1.下载下边的三个jar包,对dex2jar和jd-gui进行解压 2.将需要反编译的apk后缀名改为.rar,然后进行解压 3.将解压后生成的classes.dex加入第一步解压后的dex ...

  6. 前端缓存-IndexedDB

    IndexedDB 就是浏览器提供的本地数据库,它可以被网页脚本创建和操作.IndexedDB 允许储存大量数据,提供查找接口,还能建立索引.这些都是 LocalStorage 所不具备的.就数据库类 ...

  7. 微信小程序组件解读和分析:六、progress进度条

    progress进度条组件说明: 进度条,就是表示事情当前完成到什么地步了,可以让用户视觉上感知事情的执行.progress进度条是微信小程序的组件,和HTML5的进度条progress类似. pro ...

  8. 关于docker入门教程

    简介:docker入门教程 docker入门教程翻译自docker官方网站的Docker getting started 教程,官方网站:https://docs.docker.com/linux/s ...

  9. 关于Farseer.net轻量级ORM开源框架 V1.0 概念版本开发的消息

    V0.2版的开源距离今天(05年03月)已有近3年的时间.可以说这个版本已经有点落伍的感觉了,呵呵. V0.2版至今一直处于BUG的修复及一些细小功能的增加,所以版本号上一直没有变化. 其实在这1.2 ...

  10. Linux 控制终端转义和控制序列

    DESCRIPTION 描述 Linux控制台实现了VT102和ECMA-48/ISO 6429/ANSI X3.64终端控制的子集, 这个子集很庞大,当然还有Linux自己私有的控制序列,以改变颜色 ...