Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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".
Input
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.
Output
Print the number of segments in the bears' wall where Horace can "see an elephant".
Sample Input
13 5
2 4 5 5 4 3 2 2 2 3 3 2 1
3 4 4 3 2
2
Hint
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.
第一个kmp。。。。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<string> #define N 200005
#define M 15
#define mod 10000007
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n;
int w;
int a[N],b[N];
int c[N],d[N];
int next[N];
int ans; void get_next()
{
int i = , j = ;
next[] = ;
while(i <=w-)
{
if(j == || d[i] == d[j])
{
j++;
i++;
next[i] = j;
}
else
j = next[j];
}
// for(i=1;i<=w-1;i++){
// printf(" i=%d d=%d next=%d\n",i,d[i],next[i]);
// }
} void ini()
{
int i;
ans=;
for(i=;i<=n;i++){
scanf("%d",&a[i]);
}
for(i=;i<=n-;i++){
c[i]=a[i+]-a[i];
}
// for(i=1;i<=n-1;i++){
// printf(" %d",c[i]);
// }
for(i=;i<=w;i++){
scanf("%d",&b[i]);
}
for(i=;i<=w-;i++){
d[i]=b[i+]-b[i];
}
} void solve()
{
int i,j;
// printf(" 1221\n");
if(w==){
ans=n;
return ;
}
i=,j=;
while(i<=n-){
// printf(" i=%d j=%d ans=%d\n",i,j,ans);
if(j== || c[i]==d[j]){
if(j==w-){
ans++;
//i++;
j=next[j];
}
else{
i++;
j++;
}
}
else{
j=next[j];
}
}
} void out()
{
printf("%d\n",ans);
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
// for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
while(scanf("%d%d",&n,&w)!=EOF)
{
// if(n==0 && m==0) break;
//printf("Case %d: ",ccnt);
ini();
get_next();
solve();
out();
} return ;
}
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 说实话,这题看懂题意后秒出思路,和顺波说了一下是KMP,后来过了一会确定了思路他开始写我中途接了个电话,回来kaungbin模板一板子上去直接A了. 题意: ...
- 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 ...
随机推荐
- Xcode5 如何添加一个Github/Repository 并且Checkout
1. 添加一个Account 也就是添加一个 Repository. In Xcode, choose Xcode > Preferences, and click Accounts. Pre ...
- Python-OpenCV:cv2.imread(),cv2.imshow(),cv2.imwrite()
为什么使用Python-OpenCV? 虽然python 很强大,而且也有自己的图像处理库PIL,但是相对于OpenCV 来讲,它还是弱小很多.跟很多开源软件一样OpenCV 也提供了完善的pytho ...
- MIPS——循环语句
有关指令 add $t1,$t2,$t3 #寄存器+寄存器,$t1 = $t2 + $t3 add $t1,$t2,immediate #寄存器+立即数,$t1 = $t2 + immediate b ...
- 获取CPU相关信息
实现效果: 知识运用: WMI管理类中的ManagementObjectCollection类 ManagementObjectSearcher类的Get方法 和ManagementObje ...
- CAD交互绘制圆形云线批注(网页版)
js中实现代码说明: 动态拖放时的绘制事件: function DoDynWorldDrawFun(dX,dY,pWorldDraw,pData) { //自定义实体的GUID标识符 var sGui ...
- shrio 权限管理filterChainDefinitions过滤器配置
/** * Shiro-1.2.2内置的FilterChain * @see ============================================================= ...
- springboot 修炼之路
网上无意中发现一份关于springboot的教程说明,说的很详细,大家可以参考.具体地址:http://www.spring4all.com/article/246
- Xshell 配色方案 Ubuntu Solarized_Dark isayme
前言 最近在用Ubuntu,发现它的配色方案挺好看的,所以查了下有没有大神做过Xshell的Ubuntu配色方案. 一看,果然还是有大佬做了这个的. 三套配色配置如下: 1. Ubuntu的Solar ...
- Linux基础学习-使用PXE+Kickstart无人值守安装服务
无人值守安装系统 PXE(Preboot eXecute Environment,预启动执行环境)是由Intel公司开发的技术,可以让计算机通过网络来启动操作系统(前提是计算机上安装的网卡支持PXE技 ...
- 控制mysql数字转换
在实际工作中我们常常需要将数字进行格式化,比如将12.0073233变为12.01,或把12变为12.00,或把12变为0000012,这种格式之间的转换总结如下: 一,浮点数的转换--直接设 ...