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 ...
随机推荐
- Eclipse Neon Java版本安装Java EE插件
Help→Install New Software 地址:Neon - http://download.eclipse.org/releases/neon/201703231000 选择 Web,X ...
- ImageView加载长图(适用不需要缩放的情况)
此案例适用于加载网络长图且图片的宽和高已知的情况.由于ImageView加载图片有一个4096*4096的限制,所以对于巨长图的加载比较麻烦,需要我们自己去手动处理. 有两种解决方案:第一种就是比较l ...
- [未读]backbonejs应用程序开发
买来就没有动过,那阵子刚好离职找工作,之后学backbone的劲头就过去了= =
- CSS进阶:提高你前端水平的 4 个技巧
译者注:随着 Node.js .react-native 等技术的不断出现,和互联网行业的创业的层出不穷,了解些前端知识,成为全栈攻城师,快速的产出原型,展示你的创意,对程序员,尤其是在创业的程序员来 ...
- tcpdump 使用详解——转载
http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html 简介 用简单的话来定义tcpdump,就是:dump the traf ...
- 迅为iMX6UL Cortex-A7架构单核ARM开发板接口介绍-支持定制
支持商业级和工业级核心板 1. POWER 电源接口电源输入为 5V/2A+,给核心板提供 5V 电源,给底板供电.原理图部分如下图所示. 电源接口位置如下图所示. 2. SWITCH 电源开关轻触电 ...
- js生成随机的uuid
function generateUUID() { var d = new Date().getTime(); if(window.performance && typeof wind ...
- Python3基础教程(十九)—— 项目结构
本节阐述了一个完整的 Python 项目结构,你可以使用什么样的目录布局以及怎样发布软件到网络上. 创建Python项目 我们的实验项目名为 factorial,放到 /home/shiyanlou/ ...
- Hadoop伪集群部署
环境准备 [root@jiagoushi ~]# yum -y install lrzsz 已加载插件:fastestmirror Repository 'saltstack-repo': Error ...
- windows定时执行python脚本
from:http://blog.csdn.net/Gpwner/article/details/77882131