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 ...
随机推荐
- JS中基本的一些兼容问题 可能解释的不会太清楚
做兼容注意: 一如果两个都是属性,用逻辑||做兼容 二如果有一个是方法 用三目运算符做兼容 三多个属性或方法封装函数做兼容 一:谷歌浏览器和火狐浏览器鼠标滚动条兼容 1.document.docume ...
- Maven实战1
屏上得来终觉浅,绝知此事要躬行 总结: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px ".PingFang SC"; c ...
- 用ES6巧妙的解决传统面试中的算法小问题!
最近自己也在准备面试,在复习算法的时候,机智的用了一波ES6.一起来瞧瞧吧! 1.数组的去重 var arr=str.split(''); for(var i=0;i<arr.length-1; ...
- uva12519
The Farnsworth Parabox Professor Farnsworth, a renowned scientist that lives in year 3000 working at ...
- OpenVPN CentOS7 安装部署配置详解
一 .概念相关 1.vpn 介绍 vpn 虚拟专用网络,是依靠isp和其他的nsp,在公共网络中建立专用的数据通信网络的技术.在vpn中任意两点之间的链接并没有传统的专网所需的端到端的物理链路,而是利 ...
- mysql error 1290 hy000:The MySQL server is running with the --skip-grant-tables option so it cannot execute this statemen' 解决方案
如果在执行授权命令的时候报错 mysql> grant all privileges on *.* to root@"; ERROR (HY000): The MySQL server ...
- DB2插入数据 sqlcode302 sqlstate22001错误如何解决?
总结:出现这种错误的原因主要是,插入数据时的长度和数据库中定义的长度不匹配或超出限制.
- ZOJ2110 HDU1010 搜索 Tempter of the Bone
传送门:Tempter of the Bone 大意是给一个矩阵,叫你是否可以在给定的可走路径上不重复地走,在最后一秒走到终点. 我用了两个剪枝,且称其为简直001和剪枝002,事实证明001不要都可 ...
- 原生API实现拖拽上传文件实践
功能: 拖拽上传文件.图片,上传的进度条,能够同时上传多个文件. 完整的demo地址:https://github.com/qcer/FE-Components/tree/master/QDrag 涉 ...
- dotweb框架之旅 [二] - 常用对象-App(dotweb)
dotweb属于一个Web框架,希望通过框架行为,帮助开发人员快速构建Web应用,提升开发效率,减少不必要的代码臃肿. dotweb包含以下几个常用对象: App(dotweb) App容器,为Web ...