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

输入示例

  1.  

输出示例

  1.  

数据规模及约定

见“输入

题解

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

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cctype>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. int read() {
  10. int x = 0, f = 1; char c = getchar();
  11. while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
  12. while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
  13. return x * f;
  14. }
  15.  
  16. #define maxn 200010
  17. int n, m, S[maxn], T[maxn], Fail[maxn];
  18.  
  19. int main() {
  20. n = read(); m = read();
  21. for(int i = 1; i <= n; i++) S[i] = read();
  22. for(int i = 1; i <= m; i++) T[i] = read();
  23.  
  24. if(m == 1) return printf("%d\n", n), 0;
  25.  
  26. for(int i = 1; i < n; i++) S[i] = S[i+1] - S[i]; n--;
  27. for(int i = 1; i < m; i++) T[i] = T[i+1] - T[i]; m--;
  28. for(int i = 2; i <= m + 1; i++) {
  29. int j = Fail[i-1];
  30. while(j > 1 && T[j] != T[i-1]) j = Fail[j];
  31. Fail[i] = T[j] == T[i-1] ? j + 1 : 1;
  32. }
  33. int p = 1, ans = 0;
  34. for(int i = 1; i <= n; i++) {
  35. while(p > 1 && T[p] != S[i]) p = Fail[p];
  36. if(T[p] == S[i] && p == m) ans++;
  37. p = T[p] == S[i] ? p + 1 : 1;
  38. }
  39.  
  40. printf("%d\n", ans);
  41.  
  42. return 0;
  43. }

[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. Ubuntu Server 上安装pip后pip命令报错的解决办法

    Installation Do I need to install pip? pip is already installed if you are using Python 2 >=2.7.9 ...

  2. jmeter(十二)处理Cookie与Session

    JMeter的工作原理是: JMeter可以作为Web服务器与浏览器之间的代理网关,以便捕获浏览器的请求和Web服务器的响应,这样就很容易地生成性能测试脚本, 有了性能测试脚本,JMeter就可以通过 ...

  3. 456 132 Pattern 132模式

    给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj.设计一个算法,当 ...

  4. 里氏替换原则中is和as分别的作用

    is 是用于检查对象是否指定类型兼容 if(empls[i] is SE){ ((SE)empls).SayHi(); } as 不用强转可以直接转换 if(empls[i] is SE){ SE s ...

  5. Android学习笔记(十三) Handler

    可用于解决上一则笔记所提到的WorkerThread无法修改UI控件的问题 一.Handler.Looper和MessageQueue的基本原理 Handler把消息对象放到MessageQueue当 ...

  6. spring mvc 配置运行报错误

    四月 06, 2015 10:51:18 上午 org.apache.catalina.startup.VersionLoggerListener log 信息: Server version: Ap ...

  7. OJB

    OJB 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 对象关系桥(OJB)是一种对象关系映射工具,它能够完成从Java对象到关系数据库的透明存储.   英文名 OJB ...

  8. 深入理解python对象及属性

    类属性和实例属性首先来看看类属性和类实例的属性在python中如何存储,通过__dir__方法来查看对象的属性 >>> class Test(object): pass>> ...

  9. python 内置2to3工具将python2代码转换为python3代码

    python2与python3代码不兼容,如果需要python2代码在python3环境下运行,需要将代码进行转换,本文介绍使用python3内置工具2to3.py对代码进行转换 一:2to3.py在 ...

  10. ubuntu18.04 frpc安装与自动启动

    1. 下载, 解压 export FRP_VERSION='0.25.3' wget --no-check-certificate https://github.com/fatedier/frp/re ...