题意:给定两个序列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. hive学习3(hive基本操作)

    hive基本操作 hive的数据类型 1)基本数据类型 TINYINT,SMALLINT,INT,BIGINT FLOAT/DOUBLE BOOLEAN STRING 2)复合类型 ARRAY:一组有 ...

  2. iscroll初体验

    引入 iscroll是什么?多的概念性让人看不懂的东西就不说了因为看了也可能看不明白,iscroll主要用于移动端设备,主要包括以下的应用场合: 缩放 拉动刷新 速度和性能提升 精确捕捉元素 自定义滚 ...

  3. eclipse安装activiti工作流插件

    方式一:在有网络的情况下,安装流程设计器步骤如下: 1.点击eclipse上方工具栏的Help,选择Install New Software 2.弹出如下窗口,然后填写插件名称和安装地址 Name: ...

  4. React中props.children和React.Children的区别

    在React中,当涉及组件嵌套,在父组件中使用props.children把所有子组件显示出来.如下: function ParentComponent(props){ return ( <di ...

  5. 如何查看Linux的系统是64位的还是32位的

    可以用命令“getconf LONG_BIT”查看,如果返回的结果是32则说明是32位,返回的结果是64则说明是64位. 此外还可以使用命令“uname -a”查看,输出的结果中,如果有x86_64就 ...

  6. THEOS makefile

    转自https://www.h4ck.org.cn/2013/07/theos-makefile/ theos的makefile写法与其他linux/unix环境下的makefile写法大同小异,但是 ...

  7. 怎么在阿里云服务器部署多个tomcat

    部署前准备: 1.到阿里云官网购买一台服务器 2.给阿里云服务器挂盘,阿里云有教程这里不讲解,自己看. Linux 系统挂载数据盘 视频:Linux服务器挂载数据盘 3.下载tomcat  http: ...

  8. Linux Red hat修改主机名

    步骤一:修改etc/hosts文件内容为 [root@kypt01 /]# cd etc[root@kypt01 etc]# cat hosts127.0.0.1 localhost kypt01 l ...

  9. python数据结构之二叉树的实现

    树的定义 树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构,很象自然界中的树那样.树结构在客观世界中广泛存在,如人类社会的族谱和各种社会组织机构都可用树形 ...

  10. 阿里云里面的Linux 系统挂载数据盘

    转自:http://www.cnblogs.com/adjk/p/5112360.html 适用系统:非IO优化+SSD云盘Linux(Redhat , CentOS,Debian,Ubuntu)实例 ...