codeforces 471D MUH and Cube Walls kmp
来源:互联网 发布:淘宝退货骗局给假地址 编辑:程序博客网 时间:2024/06/15 01:43
题目链接:cf 471d
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 52 4 5 5 4 3 2 2 2 3 3 2 13 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.
/****************************************************** * File Name: d.cpp * Author: kojimai * Creater Time:2014年09月27日 星期六 00时19分37秒 ******************************************************/#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<iostream>using namespace std;#define FFF 200005long long a[FFF],b[FFF];long long c[FFF],d[FFF];int next[FFF];int main(){int n,w;cin>>n>>w;for(int i=0;i<n;i++){cin>>a[i];if(i)c[i-1]=a[i]-a[i-1];}for(int i=0;i<w;i++){cin>>b[i];if(i)d[i-1]=b[i]-b[i-1];}if(w==1){cout<<n<<endl;}//dgbug/*for(int i=0;i<n-1;i++) cout<<c[i]<<' ';cout<<endl; for(int i=0;i<w-1;i++) cout<<d[i]<<' ';cout<<endl; */else{next[0]=-1;int index=0;next[1]=0;for(int i=2;i<w;i++){while(index>=0&&d[index]!=d[i-1])index=next[index];next[i]=++index;}int ans=0;int l1=0,l2=0;while(l1<n-1){if(c[l1]==d[l2]||l2==-1){l1++;l2++;if(l2==w-1){ans++;l2=next[l2];}}elsel2 = next[l2];}cout<<ans<<endl;}return 0;}
- codeforces 471D MUH and Cube Walls kmp
- 【CodeForces】471D MUH and Cube Walls KMP
- CodeForces 471D MUH and Cube Walls(KMP匹配)
- CodeForces 471D MUH and Cube Walls KMP
- CodeForces 471D MUH and Cube Walls(KMP)
- Codeforces 471D MUH and Cube Walls【思维+KMP】
- 471D - MUH and Cube Walls (KMP)
- CodeForces 471D MUH and Cube Walls
- codeforces MUH and Cube Walls (kmp)
- 【CODEFORCES】 D. MUH and Cube Walls
- 【CodeForces】471D MUH and Cube Walls KMP或者字符串HASH
- CodeForces 471D MUH and Cube Walls KMP算法+串的取差性质
- 【字符串hash】 codeforces 471D MUH and Cube Walls
- Codeforces 471D MUH and Cube Walls 题解
- Codeforces #269 (Div. 2)D. MUH and Cube Walls(数据结构:KMP)
- Codeforces Round #269 (Div. 2) D题 MUH and Cube Walls(KMP)
- Codeforces Round #269 (Div. 2)(D. MUH and Cube Walls(KMP))
- Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!
- OpenCV基础篇之查找表
- CentOS上编译安装OpenCV-2.3.1与ffmpeg-2.1.2
- [Python学习] 专题三.字符串的基础知识
- OpenCV基础篇之读取显示图片
- OpenCV基础篇之像素操作对比度调节
- codeforces 471D MUH and Cube Walls kmp
- DSP/BIOS使用之初窥门径——滴答时钟及烧写Flash
- PCB设计资料:看到最后才知道是福利
- LabVIEW上位机与串口通信
- CentOS配置smaba与Windows共享文件
- 三极管的电平转换及驱动电路分析
- 对功率谱的一点理解
- C. MUH and House of Cards(Codeforces Round #269)
- 数组的指针特性