[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 ...
随机推荐
- Sequence POJ - 2442
Sequence POJ - 2442 口胡一个结论:就是前i行产生的最小的n个和,一定可以在"前i-1行产生的最小n个和,每一个加上这一行的任意一个数,产生的n2个数"中找到.( ...
- C# DataTable的详细用法[转]
原文链接 1.新建表 private DataTable vsDt =new DataTable(); 2.清空表中内容(表中的列还在) vsDt.Clear(); 3.清空表中的列 vsD ...
- C#关于html颜色值的转化 ColorTranslator
//颜色转换非常重要快速的转换类. ColorTranslator.FromHtml (); //从html字符串得到颜色 ColorTranslator.ToHtml();
- UML 用例图(转载)
UML是系统架构设计师考试的一个重要考点,需要考生掌握.但是有些考生,在学习的过程中会有这样的疑问,在敏捷开发时代,UML还有没有必要去学习? UML还是有用的,主要用在设计和分析阶段,但是UML不适 ...
- 微信小程序开发初次尝试-----实验应用制作(一)
初次尝试微信小程序开发,在此写下步骤以做记录和分享. 1.在网上找了很多资料,发现这位知乎大神提供的资料非常全面. 链接 https://www.zhihu.com/question/50907897 ...
- Sass的的使用三
[Sass]普通变量与默认变量普通变量定义之后可以在全局范围内使用. 默认变量sass 的默认变量仅需要在值后面加上 !default 即可.sass 的默认变量一般是用来设置默认值,然后根据需求来覆 ...
- HDU_1237_简单计算器
运算符为+,-,*,/:操作数为整数:且没有括号 设定符号优先级,先在栈底压运算符0 #include<iostream> #include<cstdio> #include& ...
- python3爬取微博评论并存为xlsx
python3爬取微博评论并存为xlsx**由于微博电脑端的网页版页面比较复杂,我们可以访问手机端的微博网站,网址为:https://m.weibo.cn/一.访问微博网站,找到热门推荐链接我们打开微 ...
- day25-1 网络架构与互联网组成
目录 网络架构 单机架构 CS架构 数据放在服务端和客户端的利与弊 BS架构 互联网和互联网的组成 互联网的硬件组成 互联网的软件组成 网络架构 单机架构 应用领域: 单机游戏 CS架构 基于网络,应 ...
- WOJ600——水题
第一次做本校OJ的题,被坑的好惨啊! 题目:600.Minimum Distance 题目大意:给定平面上3个点A.B.C,求平面上的任一顶点P,使得|PA|+2|PB|+3|PC|. 由于刚好在这 ...