Codeforces 471D MUH and Cube Walls【思维+KMP】
来源:互联网 发布:租人软件app 编辑:程序博客网 时间:2024/06/07 15:24
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 ofw towers. The bears also finished making their wall but they didn't give it a name. Their wall consists ofn 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 ofw 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 andw (1 ≤ n, w ≤ 2·105) — the number of towers in the bears' and the elephant's walls correspondingly. The second line containsn 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.
给你一个数组A,再给你一个数组B,我们可以任意将数组B整体增加或者减少值X(整数),我们可以进行修改值的操作无限次,每次我们要在数组A中找寻有几段和数组B完全匹配的子段。
思路:
如果我们枚举这个修改值X的话,时间复杂度非常的高,这里我们只要想到了差值,那么这个题就结束了。
对应数组B,我们无论对于整体修改值X为多大,其差值都是一样的:
3 4 4 3 2
-1 0 1 1
所以我们只要对于这个差值进行KMP匹配即可。
当M==1的时候需要特判,答案为N.
Ac代码:
#include<stdio.h>#include<string.h>using namespace std;int tmpa[2500050];int tmpb[2500050];int a[250050];int b[250050];int next[250050];int n,m;int lena;int lenb;int output;void set_naxt()//子串的next数组{ int i=0,j=-1; next[0]=-1; while(i<lenb) { if(j==-1||b[i]==b[j]) { i++; j++; next[i]=j; } else j=next[j]; }}int kmp(){ int i=0,j=0; set_naxt(); while(i<lena) { if(j==-1||a[i]==b[j]) { i++;j++; } else j=next[j]; if(j==lenb) { output++; j=next[j]; //printf("%d\n",j); } } return -1;}int main(){ while(~scanf("%d%d",&n,&m)) { for(int i=0;i<n;i++)scanf("%d",&tmpa[i]); for(int i=0;i<m;i++)scanf("%d",&tmpb[i]); if(m==1) { printf("%d\n",n); continue; } for(int i=0;i<n-1;i++)tmpa[i]=tmpa[i]-tmpa[i+1]; for(int i=0;i<m-1;i++)tmpb[i]=tmpb[i]-tmpb[i+1]; lena=n-1; lenb=m-1; for(int i=0;i<n-1;i++)a[i]=tmpa[i]; for(int i=0;i<m-1;i++)b[i]=tmpb[i]; output=0; kmp(); printf("%d\n",output); }}
- 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裸模板拿走!
- mac下php-fpm配置
- centos安装java
- C++中int、string等常见类型转换
- ZooKeeper配置和学习笔记
- NIO学习笔记
- Codeforces 471D MUH and Cube Walls【思维+KMP】
- css基础学习---表单学习
- C++学习笔记 lesson2 类和对象
- 卡片布局
- 11期 1月期刊自荐
- 原生js兼容性问题
- androidstudio 导入别人demo报错
- OFDM系统的关键技术及分析
- 报错unrecognized windows sockets error:0:JVM_Bind