D - 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".
13 5
2 4 5 5 4 3 2 2 2 3 3 2 1
3 4 4 3 2
2
The picture to the left shows Horace's wall from the sample, the picture to the right shows the bears' wall. The segments where Horace can "see an elephant" are in gray.
#include <bits/stdc++.h>
using namespace std;
void makeNext(const int P[],int next[],int m)
{
/*
next[i]表示前i个字符中,最大前后缀相同的长度
*/
int q,k;
next[]=;
for (q=,k=;q<m;++q)
{
while(k>&&P[q]!=P[k])
k = next[k-];
/*
这里的while循环很不好理解!
就是用一个循环来求出前后缀最大公共长度;
首先比较P[q]和P[K]是否相等如果相等的话说明已经K的数值就是已匹配到的长的;
如果不相等的话,那么next[k-1]与P[q]的长度,为什么呐?因为当前长度不合适
了,不能增长模板链,就缩小看看next[k-1]
的长度能够不能和P[q]匹配,这么一直递归下去直到找到
*/
if(P[q]==P[k])//如果当前位置也能匹配上,那么长度可以+1
{
k++;
}
next[q]=k;
}
} int kmp(const int T[],const int P[],int next[],int n,int m)
{
int i,q;
makeNext(P,next,m);
int res=;
for (i=,q=;i<n;++i)
{
while(q>&&P[q]!= T[i])
q = next[q-];
/*
这里的循环就是位移之后P的前几个字符能个T模板匹配
*/
if(P[q]==T[i])
{
q++;
}
if(q==m)//如果能匹配的长度刚好是T的长度那么就是找到了一个能匹配成功的位置
{
res++;
}
}
return res;
}
int T[];
int P[];
int next[];
int n,m;
int main(){
// freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%d",&T[i]);
}
for(int i=;i<m;i++){
scanf("%d",&P[i]);
}
for(int i=;i<n-;i++){
T[i]=T[i+]-T[i];
}
n--;
for(int i=;i<m-;i++){
P[i]=P[i+]-P[i];
}
m--;
if(m==){
printf("%d\n",n+);
return ;
}
if(n==){
printf("0\n");
return ;
}
makeNext(P,next,m);
printf("%d\n",kmp(T,P,next,n,m));
return ;
}
D - MUH and Cube Walls的更多相关文章
- 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 ...
- 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 ...
随机推荐
- Linux 内核模块程序结构
1.内核加载函数 即我们常说的内核入口函数,当内核被加载的时候调用,在内核入口函数中多进行设备的注册和初始化,其中最常用的莫过于module_init().insmod xxx.ko的时候调用. 通常 ...
- HCatalog
HCatalog HCatalog是Hadoop中的表和存储管理层,能够支持用户用不同的工具(Pig.MapReduce)更容易地表格化读写数据. HCatalog从Apache孵化器毕业,并于201 ...
- sqoop使用的问题
找不到表 17/05/02 18:15:47 ERROR tool.ImportTool: Imported Failed: There is no column found in the targe ...
- D. How many trees? DP
D. How many trees? time limit per test 1 second memory limit per test 64 megabytes input standard in ...
- SQL Server 锁机制 悲观锁 乐观锁 实测解析
先引入一些概念,直接Copy其他Blogs中的,我就不单独写了. 一.为什么会有锁 多个用户同时对数据库的并发操作时会带来以下数据不一致的问题: 1.丢失更新 A,B两个用户读同一数据并进行修改,其中 ...
- 【转】NAS 黑群晖 配置完成(不含硬盘),NAS能做什么?
在配黑群晖前,240元入手过一个艾美佳的NAS感受了下,功能倒还合适,就是配置太老,厂家固件也停止更新了,一直不太满意. 后来经常关注NAS1,发现现在X86的NAS也很好自己DIY了,就长草了,向女 ...
- IDL 使用数组
1.下标方式 IDL> array=indgen(8) IDL> print,array 0 1 2 3 4 5 6 7 IDL> print,array[3] IDL> ar ...
- HTML与标记属性
网站部分:UI:AI.PS 前端:html.css.js 网站:是一个存放在网络服务器上的完整信息的集合体.由域名.空间服务器.网站程序.数据库等组成.由多个网页以一定的方式连接在一起,成为一个整体. ...
- 当谈到 GitLab CI 的时候,我们都该聊些什么(下篇)
上篇主要介绍了 GitLab WorkFlow 以及 CI/CD 做的事情,并且详细分析 GitLab CI 跟 Runner 信息交互是如何进行的.接下来将为大家讲解 Executor 的实现,再通 ...
- .net Mvc框架原理
.net Mvc框架原理 本文只是简要说明原理,学习后的总结. 1.当一个Http请求发送后会被URLRoutingModule拦截(这时候也就是正式进入管道,下章会讲管道事件) 2.这时根据Isap ...