题意:给定两个序列a ,b, 如果在a中存在一段连续的序列使得
a[i]-b[0]==k, a[i+1]-b[1]==k.... a[i+n-1]-b[n-1]==k
就说b串在a串中出现过!最后输出b串在a串中出现几次!

思路: KMP变形!如何转换成KMP求解呢?
举一个例子说明一下:
a: 5 10 8 10 11 9 11 12 10 15
b: 4 2 4 5 3
根据题意 a中的 10 8 10 11 9 与 b是匹配的, 11 9 11 12 10跟b也是匹配的!
如何将b串以及 10 8 10 11 9, 以及 11 9 11 12 10转换成同一个串?这样好套用kmp啊!
因为对应的数值的差值都是相同的! 令a[i]-=a[i+1], b[i]-=b[i+1]!
这样也就是将串的长度减少了1,这样就可以套kmp模板了!
别忘记对特殊情况考虑一下!

 #include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 200005
using namespace std; int f[N], a[N], b[N];
int n, w;
void getFail(){
f[]=; f[]=;
for(int i=; i<w; ++i){
int j=f[i];
while(j && b[i] != b[j]) j=f[j];
if(b[i] == b[j]) f[i+] = j+;
else f[i+] = ;
}
} void findText(){
int j=;
int cnt = ;
for(int i=; i<n; ++i){
while(j && a[i] != b[j]) j=f[j];
if(a[i] == b[j]) ++j;
if( j == w ) ++cnt;
}
printf("%d\n", cnt);
} int main(){
scanf("%d%d", &n, &w);
for(int i=; i<n; ++i){
scanf("%d", &a[i]);
if(i) a[i-] -= a[i];
} for(int i=; i<w; ++i){
scanf("%d", &b[i]);
if(i) b[i-] -= b[i];
}
if(n== && w==){
printf("%d\n", );
return ;
}
else if(n==){
printf("%d\n", );
return ;
}
else if( w== ){
printf("%d\n", n);
return ;
}
--n;
--w;
b[w] = -1e6;
getFail();
findText();
return ;
}

codeforces MUH and Cube Walls的更多相关文章

  1. Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp

    D - MUH and Cube Walls Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  2. Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!

    D. MUH and Cube Walls 说实话,这题看懂题意后秒出思路,和顺波说了一下是KMP,后来过了一会确定了思路他开始写我中途接了个电话,回来kaungbin模板一板子上去直接A了. 题意: ...

  3. D - MUH and Cube Walls

    D. MUH and Cube Walls   Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant ...

  4. [codeforces471D]MUH and Cube Walls

    [codeforces471D]MUH and Cube Walls 试题描述 Polar bears Menshykov and Uslada from the zoo of St. Petersb ...

  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 ...

  6. Codeforces 471 D MUH and Cube Walls

    题目大意 Description 给你一个字符集合,你从其中找出一些字符串出来. 希望你找出来的这些字符串的最长公共前缀*字符串的总个数最大化. Input 第一行给出数字N.N在[2,1000000 ...

  7. CodeForces–471D--MUH and Cube Walls(KMP)

    Time limit         2000 ms  Memory limit  262144 kB Polar bears Menshykov and Uslada from the zoo of ...

  8. MUH and Cube Walls

    Codeforces Round #269 (Div. 2) D:http://codeforces.com/problemset/problem/471/D 题意:给定两个序列a ,b, 如果在a中 ...

  9. CF471D MUH and Cube Walls

    Link 一句话题意: 给两堵墙.问 \(a\) 墙中与 \(b\) 墙顶部形状相同的区间有多少个. 这生草翻译不想多说了. 我们先来转化一下问题.对于一堵墙他的向下延伸的高度,我们是不用管的. 我们 ...

随机推荐

  1. FreeCodeCamp 前端初级算法(个人向)

    freecodecamp 初级算法地址戳这里 Reverse a String 翻转字符串 function reverseString(str) { str=str.split("&quo ...

  2. ASP怎么解除文件上传200kb限制

    第一步:修改IIS设置,允许直接编辑配置数据库.打开,Internet信息服务第二步:先在服务里关闭iis admin service服务,找到windows\system32\inetsrv\下的m ...

  3. 【迁移】—Entity Framework实例详解

    好久没有在博客园更新博客了,如今都换了新公司.前段时间写了关于EF迁移的文档,今天拿出来作为这个系列的一篇吧. 一.Entity Framework 迁移命令(get-help EntityFrame ...

  4. linux下redis设置密码登录

    redis设置密码访问 你的redis在真是环境中不可以谁想访问就可以访问,所以必须要设置密码 设置密码的流程如下: vim  /etc/redis.conf #requirepass foobare ...

  5. windows 2012 试用180天

    windows server 2012 官方下载,可以使用180天, 快到期的时候执行以下命令 slmgr.vbs -rearm

  6. find the peak value

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  7. libev代码

    就是贴上来: ev.c: /* * libev event processing core, watcher management */ /* this big block deduces confi ...

  8. Xshell小技巧

    1. 鼠标右键粘贴 工具->选项->鼠标->向右按钮->(paste the clipboard contents.) 2. 选定文本自动复制到剪贴板 工具->选项-&g ...

  9. React直出实现与原理

    前一篇文章我们介绍了虚拟DOM的实现与原理,这篇文章我们来讲讲React的直出. 比起MVVM,React比较容易实现直出,那么React的直出是如何实现,有什么值得我们学习的呢? 为什么MVVM不能 ...

  10. 前沿技术解密——VirtualDOM

    作为React的核心技术之一Virtual DOM,一直披着神秘的面纱. 实际上,Virtual DOM包含: Javascript DOM模型树(VTree),类似文档节点树(DOM) DOM模型树 ...