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 ...
随机推荐
- JSP分页技术的实现(利用当前页进行前后加减,并利用href进行当前页面传值,传值当然是那个当前值变量)
一.可滚动结果集 Connection con = DriverManager.getConnection(); PreparedStatement stmt = con.prepareStat ...
- OGG How to Resync Tables / Schemas on Different SCN s in a Single Replicat
To resync one or more tables/schemas on different SCN's using a single or minimum number of replicat ...
- python正则表达式多次提取数据(一个规则提取多组数据)
import re ttt='"FileName":"陈雪凝 - <em>绿色<\/em>","AlbumID":& ...
- TRUNCATE DELETE DROP 区别
TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系统和事务日志资源 ...
- 所有DOM元素加载之前执行的页面加载事件[jquery]
<script type="text/javascript"> (function() { alert("DOM还没加载"); })(jQuery) ...
- 更新github上的代码
昨晚熬夜写完了"git上传本地项目代码到github"的任务,早上来公司先把早上的工作完成后,抽点时间继续来继续更新文章 更新github上的代码 一.克隆代码 1.把大神的代码c ...
- 8.2.6 PEB —— PEB结构值不正确的问题
书中作者使用 dt _PEB xxxxxx 命令来查看当前进程的PEB结构. 实际操作后PEB结构显示的成员值: 作为进程链表的LDR结构居然没有值,这显然是不正常的,地址也没有输错,问题到底出在哪里 ...
- 如何使用capedit分割数据包文件
wireshark是一个网络数据包的分析工具,主要用来捕获网卡上的数据包并显示数据包的详细内容.在处理一些大的数据包文件时,如果直接用wireshark图形工具打开一些大文件的数据包会出现响应慢甚至没 ...
- mysql-mmm 部署高可用集群
集群的部署(mysql-mmm + 主从同步) 什么是集群? 多台服务器提供相同的服务 集群的好处? 节省成本. 配置集群? 准备环境:准备4台数据库服务器 准备服务器,能够ping 4 台数据库服务 ...
- mysql登录(linux)
一.修改配置文件 查询路径 find / -name my.cnf 修改文件 vi my.cnf [mysqld]下面加上skip-grant-tables 二.登录数据库 重启mysql servi ...