Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!
说实话,这题看懂题意后秒出思路,和顺波说了一下是KMP,后来过了一会确定了思路他开始写我中途接了个电话,回来kaungbin模板一板子上去直接A了。
题意:有两座城堡,每座城堡有一定的形状分别由高度区分。求第一座城堡中有多少个区间的形状和第二座城堡相似。连续一段区间高度可以任意变,但他们的相对位置是不变的。
思路:基于一段连续区间无论怎么变他们的相对位置不会变,所以分别将两个序列相邻的元素作差,一段区间的相对位置不会变,所以正好可以用差值去匹配。所以KMP模式匹配很好解决了这个问题,注意数组用int 型。
const int N=2e5+7;
int len1,len2,a[N],b[N],f[N],p[N];
void kmp_pre(int *x,int m,int *next)//以下感谢kuangbin神主提供的模板。
{
int i,j;
j=next[0]=-1;
i=0;
while(i<m)
{
while(-1!=j&&x[i]!=x[j]) j=next[j];
next[++i]=++j;
}
}
void prekmp(int *x,int m,int *kmpnext)
{
int i,j=-1;
kmpnext[0]=-1;
i=0;
while(i<m)
{
while(-1!=j&&x[i]!=x[j]) j=kmpnext[j];
if(x[++i]==x[++j]) kmpnext[i]=kmpnext[j];
else kmpnext[i]=j;
}
}
int next[N];
int kmp_count(int *x,int m,int *y,int n)
{
int i,j,ans=0;
kmp_pre(x,m,next);
i=j=0;
while(i<n)
{
while(-1!=j&&y[i]!=x[j]) j=next[j];
i++,j++;
if(j>=m)
{
ans++;
j=next[j];
}
}
return ans;
}
int main()
{
int w,n;
while(~scanf("%d%d",&w,&n))
{
len1=0,len2=0;
for(int i=0;i<w;i++)
{
scanf("%d",&a[i]);
if(i) f[len1++]=a[i]-a[i-1];
}
for(int i=0;i<n;i++)
{
scanf("%d",&b[i]);
if(i) p[len2++]=b[i]-b[i-1];
}
if(n==1)
{
printf("%d\n",w);
continue;
}
int ans=kmp_count(p,len2,f,len1);
printf("%d\n",ans);
}
return 0;
}
正确快速读懂题意很重要,思路清晰想到了就是模板题。
Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!的更多相关文章
- 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) B. MUH and Important Things
It's time polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from t ...
- Codeforces Round #269 (Div. 2) A B C
先说C 题目链接:http://codeforces.com/problemset/problem/471/C 题目意思:有 n 张卡,问能做成多少种不同楼层(floor)的 house.注意这 n ...
- Codeforces Round #269 (Div. 2) A,B,C,D
CodeForces - 471A 首先要有四个数相等,然后剩下两个数不同就是Bear,否则就是Elephant. #include <bits/stdc++.h> using names ...
- Codeforces Round #269 (Div. 2)
A 题意:给出6根木棍,如果有4根相同,2根不同,则构成“bear”,如果剩余两个相同,则构成“elephant” 用一个数组分别储存各个数字出现的次数,再判断即可 注意hash[i]==5的时候,也 ...
- 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 ...
- MUH and Cube Walls
Codeforces Round #269 (Div. 2) D:http://codeforces.com/problemset/problem/471/D 题意:给定两个序列a ,b, 如果在a中 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- 转 ORACLE数据库ORA-00392 log 4 of thread 1 is being cleared, operation not allowed错误
现象: 数据库在做to-time recovery, 时候,restore and recover 都是正常的,但是最后一步open resetlogs 报错如下 ORA-00392 原因: 因为是在 ...
- Hadoop工作流概念学习系列总述(一)
不多说,这里,直接上干货!从这篇博客起,逐步分享如下: 1.工作流 2.Hadoop工作流(内置) 3.第三方框架--Azkaban(推荐外安装)
- windows 7 正确禁用 IPv6
与Windows XP和Windows Server 2003不同的是,Windows Vista和Windows Server 2008中的IPv6无法被卸载.然而,在Windows Vista和W ...
- JS编写自己的富文本编辑器
富文本编辑器,网上有很多功能齐全种类丰富的如百度的Ueditor,简单适用型的如WangEditor等等.在经过一番挑选后,我发现都不适用现在的项目,然后决定自己造轮子玩玩.富文本编辑器中主要涉及到J ...
- 【HEVC帧间预测论文】P1.7 Content Based Hierarchical Fast Coding Unit Decision Algorithm
Content Based Hierarchical Fast Coding Unit Decision Algorithm For HEVC <HEVC标准介绍.HEVC帧间预测论文笔记> ...
- ubuntu上部署windows开发的dotnet core程序
目标:完成windows上开发的dotnet core程序部署至linux服务器上(Ubuntu 14.04) windows上开发dotnet core很简单,安装好VS2017,建立相关类型的项目 ...
- liunx中安装软件的几种方式
服务器安装包一般有四种方式 1.源代码包安装 自由度高 需要预编译,安装速度慢 2.rpm包手动安装 安装的缺点是文件的关联性太大 3. 二进制tar.gz格式 直接解压即可 如tomca ...
- js 数组元素排序?
Part.1 sort 方法 js 有自带排序方法 sort(), 默认 升序 排列 如: data() { return { arr: [1,3,2,5,6,8,7,4,9] } }, 控制台如 ...
- 制作JPEGImages出现的bug
我用的是下面这个脚本进行改名字: import os import sys path = "/home/bnrc/py-faster-rcnn/data/VOCdevkit2007/VOC2 ...
- Opencv竟然有中文资料
最近对于OpenCV看的较多,竟然不知不觉找到了一个中文网站,对于母语真的桥开心的嘻嘻 直方图均衡化: http://www.opencv.org.cn/opencvdoc/2.3.2/html/do ...