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中_TextView属性的XML详解 包括单行显示等等。
<pre name="code" class="html">属性名称 描述 android:autoLink 设置是否当文本为URL链接/email ...
- 填坑 | .NET core项目远程部署后连接数据库 mysql表大小写敏感问题
欣喜成功部署了项目之后又遭遇重创hhh,swagger调试数据库,报错 MySql.Data.MySqlClient.MySqlException(0x80004005) 我猜是大小写的问题,一查果然 ...
- 1090 Highest Price in Supply Chain (25 分)(模拟建树,找树的深度)牛客网过,pat没过
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- P3311 [SDOI2014]数数 AC自动机+数位DP
题意 给定一个正整数N和n个模式串,问不大于N的数字中有多少个不包含任意模式串,输出对\(1e^9+7\)取模后的答案. 解题思路 把所有模式串都加入AC自动机,然后跑数位DP就好了.需要注意的是,这 ...
- indexOf原理,Java,javascript,python实现
简介 最近做项目的时候,发现无论是前端还是后端,indexOf出现的概率都非常频繁,今天我们来看下他的实现原理吧! indexOf的含义:给定一个字符串去匹配另一个字符串的下标,如果匹配到,返回下 ...
- vue中实现后台管理路由标签页
<template> <section> <div class="navTabList el-tabs__nav-scroll" id="t ...
- Git 实用基础(配置,建库,提交,推送 GitHub)
Git 实用基础(配置,建库,提交,推送 GitHub) SVN ? Git ? 目前市面上主流的版本控制系统就是 SVN 和 Git . 两者的区别简单通俗地说就是,版本数据是否有在本地. 如果觉得 ...
- Python 字符串去除相邻重复的元素
1 def quchong(S): 2 str1=[""] 3 for i in S: 4 if i == str1[-1]: 5 str1.pop() 6 else: 7 str ...
- python之读取yaml数据
一.yaml简介 yaml:一种标记语言,专门用来写配置文件. 二.yaml基础语法 区分大小写: 使用缩进表示层级关系: 使用空格键缩进,而非Tab键缩进 缩进的空格数目不固定,只需要相同层级的元素 ...
- mysqli报错注入常见函数
以下均摘自<代码审计:企业级Web代码安全架构>一书 1.floor() select * from test where id=1 and (select 1 from (select ...