Codeforces 471D MUH and Cube Walls 题解
来源:互联网 发布:深圳美工师到哪考 编辑:程序博客网 时间:2024/06/14 21:50
题意
给定两个序列,在第一个序列中找与第二个序列相邻两数之差相同的连续子序列,能找到多少个
思路
先分别对两个子序列相邻两数做差,跑KMP,注意特判第二个序列长度为1的情况
代码
#include <cstdio>int a[200001],aa[200001],b[200001],bb[200001],next[200001];int n,w;void makenext(){ int q,k; next[0]=0; for(q=1,k=0;q<w-1;q++) { while(k>0&&bb[q]!=bb[k]) k=next[k-1]; if(bb[q]==bb[k]) k++; next[q]=k; }}int kmp(){ int cnt=0,i,q; for(i=0,q=0;i<n-1;i++) { while(q>0&&(bb[q]!=aa[i]||q==w-1)) q=next[q-1]; if(bb[q]==aa[i]) q++; if(q==w-1) { cnt++; } } return cnt;}int main(){ scanf("%d%d",&n,&w); for(int i=0;i<n;i++) scanf("%d",&a[i]); for(int i=0;i<w;i++) scanf("%d",&b[i]); for(int i=0;i<n-1;i++) aa[i]=a[i+1]-a[i]; for(int i=0;i<w-1;i++) bb[i]=b[i+1]-b[i]; makenext(); if(w==1) printf("%d\n",n); else printf("%d\n",kmp()); return 0;}
阅读全文
0 0
- Codeforces 471D MUH and Cube Walls 题解
- CodeForces 471D MUH and Cube Walls
- 【CODEFORCES】 D. MUH and Cube Walls
- codeforces 471D MUH and Cube Walls kmp
- 【CodeForces】471D MUH and Cube Walls KMP
- 【字符串hash】 codeforces 471D 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】
- 471D - MUH and Cube Walls (KMP)
- D. MUH and Cube Walls(Codeforces Round #269)
- Codeforces Round #269 (Div. 2) D.MUH and Cube Walls
- 【CodeForces】471D MUH and Cube Walls KMP或者字符串HASH
- CodeForces 471D MUH and Cube Walls KMP算法+串的取差性质
- codeforces MUH and Cube Walls (kmp)
- MUH and Cube Walls
- Codeforces #269 (Div. 2)D. MUH and Cube Walls(数据结构:KMP)
- emwin在SDRAM中绘制图形,直接加载到LCD速度比较块
- android 相对布局RelativeLayout中的一些属性的使用和实例
- 欢迎使用CSDN-markdown编辑器
- wamp中浏览器直接跳转到项目解决方法
- Android常用控件(ImageView)
- Codeforces 471D MUH and Cube Walls 题解
- 遇见Kotlin
- js 中的this
- Eclipse常用快捷
- java方法是传值还是传引用问题
- ogre 世界坐标、本地坐标、屏幕坐标之间的转换
- HDU1083 Courses(二分图匹配)
- Leetcode110. Balanced Binary Tree
- [vim] 删除