Link

一句话题意:

给两堵墙。问 \(a\) 墙中与 \(b\) 墙顶部形状相同的区间有多少个.

这生草翻译不想多说了。

我们先来转化一下问题。对于一堵墙他的向下延伸的高度,我们是不用管的。

我们只需要考虑的是上边延伸的高度,那我们可以求出相邻的两个块之间的高度差。

我们要求的是 \(a\) 中连续的一段与 \(b\) 完全相同的数量。

其实就是 \(kmp\) 匹配啦。

注意特判一下 \(m=1\) 的情况。

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define int long long
const int inf = 2147483647;
const int N = 3e5+10;
int n,m,ans,h[N],t[N],a[N],b[N],net[N];
inline int read()
{
int s = 0,w = 1; char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-') w = -1; ch = getchar();}
while(ch >= '0' && ch <= '9'){s = s * 10 + ch - '0'; ch = getchar();}
return s * w;
}
signed main()
{
n = read(); m = read();
if(m == 1) {printf("%d\n",n); return 0;}
for(int i = 1; i <= n; i++) h[i] = read();
for(int i = 1; i <= m; i++) t[i] = read();
for(int i = 1; i <= n-1; i++) a[i] = h[i] - h[i+1];//求一下相邻的两个的高度差
for(int i = 1; i <= m-1; i++) b[i] = t[i] - t[i+1];
b[m] = -inf;
int j = 0;
for(int i = 2; i < m; i++)//kmp
{
while(j && b[i] != b[j+1]) j = net[j];
if(b[i] == b[j+1]) j++;
net[i] = j;
}
j = 0;
for(int i = 1; i < n; i++)
{
while(j && a[i] != b[j+1]) j = net[j];
if(a[i] == b[j+1]) j++;
if(j == m-1) ans++;
}
printf("%d\n",ans);
return 0;
}

CF471D 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. [codeforces471D]MUH and Cube Walls

    [codeforces471D]MUH and Cube Walls 试题描述 Polar bears Menshykov and Uslada from the zoo of St. Petersb ...

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

  6. 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串中出现过!最后输出 ...

  7. MUH and Cube Walls

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

  8. Codeforces 471 D MUH and Cube Walls

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

  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. python执行gradle脚本

    import os import shutil import subprocess #拷贝文件 def copyFile(srcFile, dstFile): #检查源文件是否存在 if not os ...

  2. HDU多校1003-Divide the Stones(构造)

    Problem Description There are n stones numbered from 1 to n.The weight of the i-th stone is i kilogr ...

  3. 3 path核心模块

    const path = require('path') // require('./static/test/test') { /* 总结: __dirname: 获得当前执行文件所在目录的完整目录名 ...

  4. robotframework自动化测试框架搭建及问题汇总

    1.安装python RF框架是基于python 的,所以一定要有python环境,python与rf存在兼容性问题,我安装的是python3.7.5,robotframework3.1.2. 选择添 ...

  5. myeclipse操作hdfs

    myeclipse与hadoop集成我就不说了,还是简单说两句吧! 1.hadoop搭建完毕且可以正常访问(Linux搭建的集群环境 主机是windos) 2.在主机上将hadoop包解压 3.通过m ...

  6. format的实现

    var format = function(s, arg0) { var args = arguments; return s.replace(/\{(\d+)\}/ig, function(a, b ...

  7. Lambda表达式看这篇基本就够用了

    本文讯] 2020.05.08  polo  写博不易,尊重知识! Lambda  是java8 引入的一个新特性,闭包,又叫函数式接口,下面介绍下,常用的lambda表达式方式: 所谓的将函数作为一 ...

  8. 微信小程序-组件-视图容器

    1.view 1.作用:类似 html 的 div 用来进行页面布局,具有块级盒子特性. 2.常用属性:设置view盒子点击后的状态,以及控制是否影响父盒子的点击状态 3.eg:<view ho ...

  9. Redis源码笔记--服务器日志和函数可变参数处理server.c

    前言 Redis源码中定义了几个和日志相关的函数,用于将不同级别的信息打印到不同的位置(日志文件或标准输出,取决于配置文件的设置),这些函数的定义位于 server.h 和server.c 文件中,包 ...

  10. redis加固:修改默认端口6379、密码不为空、内网服务器访问

    一.windows下redis加固: 1.修改redis密码+修改redis端口 1.1.找到服务当中redis的属性配置文件是哪个   这个是我的"D:\Program Files\Red ...