CF471D MUH and Cube Walls
一句话题意:
给两堵墙。问 \(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的更多相关文章
- D - MUH and Cube Walls
D. MUH and Cube Walls Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant ...
- 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 & % ...
- Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!
D. MUH and Cube Walls 说实话,这题看懂题意后秒出思路,和顺波说了一下是KMP,后来过了一会确定了思路他开始写我中途接了个电话,回来kaungbin模板一板子上去直接A了. 题意: ...
- [codeforces471D]MUH and Cube Walls
[codeforces471D]MUH and Cube Walls 试题描述 Polar bears Menshykov and Uslada from the zoo of St. Petersb ...
- 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 ...
- 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串中出现过!最后输出 ...
- MUH and Cube Walls
Codeforces Round #269 (Div. 2) D:http://codeforces.com/problemset/problem/471/D 题意:给定两个序列a ,b, 如果在a中 ...
- Codeforces 471 D MUH and Cube Walls
题目大意 Description 给你一个字符集合,你从其中找出一些字符串出来. 希望你找出来的这些字符串的最长公共前缀*字符串的总个数最大化. Input 第一行给出数字N.N在[2,1000000 ...
- CodeForces–471D--MUH and Cube Walls(KMP)
Time limit 2000 ms Memory limit 262144 kB Polar bears Menshykov and Uslada from the zoo of ...
随机推荐
- 【Android】Scrollview返回顶部,快速返回顶部的功能实现,详解代码。
作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://weibo.com/mcxiaobing 首先给大家看一下我们今天这个最终实现 ...
- ParticleSystem 介绍
ParticleSystem 介绍 http://gad.qq.com/article/detail/31724
- 文件操作 -- 生成java文件
import hashlibimport os def genJavaFile(packageName, soFile): className, suffix = soFile.split('. ...
- JavaWeb三大器(过滤器、拦截器、监听器)概念梳理
最近工作碰到了一个问题:项目A需要收集项目B中的用户活跃数信息,最后通过HttpSessionAttributeListener实现.在开发过程中,网上查找了过滤器.拦截器.监听器的帖子,这里对自己收 ...
- 15_Web框架-mini frame
1.WSGI协议概述(Python Web Server Gateway Interface) 1.WSGI允许开发者将选择web框架和web服务器分开,可以混合匹配web服务器和web框架,选择一个 ...
- CLTPHP 漏洞
前言 awd小组的第一次训练 0x01 首先看一下主界面 使用的应该是PHP模板,随便翻一下找到一个注册界面 随便注册一个用户,登陆后在设置里找到一个上传点 上传我们的一句话木马 查看返回包,上传成功 ...
- Tooltip鼠标hover放上时文字提示
使用content属性来决定hover时的提示信息. 由placement属性决定展示效果: placement属性值为: 方向-对齐位置: 四个方向:top.left ...
- 论文:Show and Tell: A Neural Image Caption Generator-阅读总结
Show and Tell: A Neural Image Caption Generator-阅读总结 笔记不能简单的抄写文中的内容,得有自己的思考和理解. 一.基本信息 标题 作者 作者单位 发表 ...
- Oracle sqlplus中退格键、DEL键、上下左右键无法使用乱码问题
功能描述:Oracle sqlplus中退格键.DEL键.上下左右键无法使用乱码 1.安装readline-8.0 ①下载readline-8.0.tar.gz文件,百度网盘下载路径: https:/ ...
- web网站——apache和nginx对比02
nginx介绍 Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理 ...