codeforces471D——MUH and Cube Walls(KMP)
来源:互联网 发布:清华五道口考研知乎 编辑:程序博客网 时间:2024/05/17 23:50
给你两个柱状图,需要在第一个图里找到第二个图形状的个数
由于第二个图可以上下移动,所以只要区间内的差值相等即可。
所以求出相邻两个柱体的差值然后就是字符串匹配的问题了。
#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>using namespace std;const int MAXN=3000010;int a[MAXN];int b[MAXN];int n,w;int nextval[MAXN];void get_nextval(){ int j=1,k=0; nextval[1]=0; while(j<=w){ if(k==0||b[j]==b[k]){ k++; j++; nextval[j]=k; } else k=nextval[k]; }}int x[MAXN],y[MAXN];int main(){ scanf("%d%d",&n,&w); for(int i=1;i<=n;i++){ scanf("%d",x+i); } for(int i=1;i<=w;i++){ scanf("%d",y+i); } if(w==1){ printf("%d\n",n); return 0; } n--,w--; for(int i=1;i<=n;i++){ a[i]=x[i+1]-x[i]; } for(int i=1;i<=w;i++){ b[i]=y[i+1]-y[i]; } get_nextval(); //for(int i=1;i<=w;i++) // printf("%d ",nextval[i]); printf("\n"); int i=1,j=1; int cnt=0; while(i<=n){ if(a[i]==b[j]||j==0){ i++; j++; } else j=nextval[j]; if(j==w+1){ j=nextval[j]; cnt++; } } printf("%d\n",cnt);}
0 0
- codeforces471D——MUH and Cube Walls(KMP)
- codeforces 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)
- MUH and Cube Walls
- 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】
- CF471D MUH and Cube Walls(差分+KMP)
- Codeforces Round #269 (Div. 2)(D. MUH and Cube Walls(KMP))
- Codeforces #269 (Div. 2)D. MUH and Cube Walls(数据结构:KMP)
- Codeforces Round #269 (Div. 2) D题 MUH and Cube Walls(KMP)
- Codeforces #269 (Div. 2)D. MUH and Cube Walls(KMP)
- 【CODEFORCES】 D. MUH and Cube Walls
- CodeForces 471D MUH and Cube Walls
- 【CodeForces】471D MUH and Cube Walls KMP或者字符串HASH
- 使用AdaptiveTrigger实现自适应布局
- \r与\n的区别
- 关闭vultr账户方法
- [架构之美]一款APP从"想法-开发-上线-产品"的全过程
- 一位资深的老玩家,畅聊当年与linux的趣事
- codeforces471D——MUH and Cube Walls(KMP)
- POJ 3268 - Silver Cow Party dijkstra+转置矩阵
- The Game of iGameGuardian7.3.1
- 学习Django之创建第一个项目
- 用 Ansible 来交付 Vagrant 实例
- JS定时器来间断性的执行函数
- 选择排序
- Java基础学习应用_数组常用方法与排序(一)
- 碎片