[codeforces471D]MUH and Cube Walls
[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".
输入示例
输出示例
数据规模及约定
见“输入”
题解
两个序列差分一下后跑 KMP。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 200010
int n, m, S[maxn], T[maxn], Fail[maxn]; int main() {
n = read(); m = read();
for(int i = 1; i <= n; i++) S[i] = read();
for(int i = 1; i <= m; i++) T[i] = read(); if(m == 1) return printf("%d\n", n), 0; for(int i = 1; i < n; i++) S[i] = S[i+1] - S[i]; n--;
for(int i = 1; i < m; i++) T[i] = T[i+1] - T[i]; m--;
for(int i = 2; i <= m + 1; i++) {
int j = Fail[i-1];
while(j > 1 && T[j] != T[i-1]) j = Fail[j];
Fail[i] = T[j] == T[i-1] ? j + 1 : 1;
}
int p = 1, ans = 0;
for(int i = 1; i <= n; i++) {
while(p > 1 && T[p] != S[i]) p = Fail[p];
if(T[p] == S[i] && p == m) ans++;
p = T[p] == S[i] ? p + 1 : 1;
} printf("%d\n", ans); return 0;
}
[codeforces471D]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了. 题意: ...
- 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 ...
- CF471D MUH and Cube Walls
Link 一句话题意: 给两堵墙.问 \(a\) 墙中与 \(b\) 墙顶部形状相同的区间有多少个. 这生草翻译不想多说了. 我们先来转化一下问题.对于一堵墙他的向下延伸的高度,我们是不用管的. 我们 ...
- CodeForces–471D--MUH and Cube Walls(KMP)
Time limit 2000 ms Memory limit 262144 kB Polar bears Menshykov and Uslada from the zoo of ...
随机推荐
- Oracle10g修改数据库字符集
Oracle10g修改字符集记录: 版本:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production 参考 ...
- 198 House Robber 打家劫舍
你是一个专业的强盗,计划抢劫沿街的房屋.每间房都藏有一定的现金,阻止你抢劫他们的唯一的制约因素就是相邻的房屋有保安系统连接,如果两间相邻的房屋在同一晚上被闯入,它会自动联系警方.给定一个代表每个房屋的 ...
- 用nowrap实现div内的元素不换行
在编写自定义插件my-slider的过程中,发现无论float还是inline-block均不能保证div内的内容不换行(这两个属性在内容超出容器尺寸后,均将剩余内容做换行处理),在浏览器内比较了自己 ...
- nginx for ubuntu
1.创建文件夹 :mkdir nginx 2.解压nginx: tar zxvf nginx.gz.tar 3.nginx 初始化:在nginx的路径下执行:./configure 有可能会报错: . ...
- redis-cli 工具--raw参数的作用
最近阅读了以redis官网关于--raw参数的解释,其功能有两个: 1.按数据原有格式打印数据,不展示额外的类型信息 例如:使用命令发送方式(redis在使用时有命令发送方式和交互方式两种)创建一个k ...
- mybatis 返回值
转载: 在使用ibatis插入数据进数据库的时候,会用到一些sequence的数据,有些情况下,在插入完成之后还需要将sequence的值返回,然后才能进行下一步的操作. 使用ibatis的 ...
- WebAPI中Area的使用
很简单,创建area后,添加一下代码到AreaRegistration中即可 context.Routes.MapHttpRoute( name: "api_default", r ...
- (三)Redis for StackExchange.Redis
目录 (一)Redis for Windows正确打开方式 (二)Redis for 阿里云公网连接 (三)Redis for StackExchange.Redis StackExchange.Re ...
- android studio 调试安装
最新的手机无法debug安装调试. vivo x21 oppo r11等 解决办法: 1,关闭instant run Android Studio->Preferences->Build ...
- ubuntu 普通用户运行virt-manager时libvirt权限设置
error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': Permission deniederror: failed t ...