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. unity webview

    uniwebview http://uniwebview.onevcat.com/manual Unity3D研究院之在Android中打开WebView(三十) http://www.xuanyus ...

  2. Unity代码混淆

    https://www.zhihu.com/question/25414422 http://blog.csdn.net/kun1234567/article/details/7917847 http ...

  3. MySQL 外部联结 内连接、左右外连接辨析

    内连接 在进行跨表内连接查询数据时,查询结果只返回符合查询条件的数据:跨表内连接查询的结果和使用where的多表查询结果相同,其实就是普通的查询,没啥好说的 -- 语法: SELECT 别名1.字段名 ...

  4. ACwing 你能回答这些问题吗(线段树求最大连续字段和)

    给定长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“1 x y”,查询区间 [x,y] 中的最大连续子段和,即 maxx≤l≤r≤ymaxx≤l≤r≤y{∑ri=lA[i]∑i=l ...

  5. 12_Python语法示例(函数)

    1.写一个函数mysum,此函数带有两个参数x,y打印出两个参数x,y的和 def mysum(x, y): print(x + y) mysum(3, 2) 2.写一个函数print_even,传入 ...

  6. Agumater 爬虫进度带上了百分比,消除了.0

  7. HTML标签语言一览表

    <html> ● 文件声明 让浏览器知道这是 html 文件 <head> ● 开头 提供文件整体资讯 <title> ● 标题 定义文件标题,将显示于浏览顶端 & ...

  8. 完美激活PyCharm教程

    1.版本 本文中pycharm版本为PyCharm Professional-2018.3.3,JetbrainsCrack版本为4.2.需要注意,不同版本的pycharm对应的JetbrainsCr ...

  9. ctfhub sql注入字符型

    手工注入 1, 检查是否存在注入 2.猜字段数.列数 3.获得注入点,数据库名称,数据库版本 4.获得表名 5.获得字段名 6.获得flag sqlmap方法 1.查数据库库名 2.查表名 3.查字段 ...

  10. EasyCode插件使用及模板参考

    EasyCode插件使用及模板参考 1.介绍安装 Easycode是idea的一个插件,可以直接对数据的表生成entity.controller.service.dao.mapper无需任何编码,简单 ...