题意:给定两个序列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. Struts2学习笔记-jsp中引用struts2框架

    如果在jsp中需要引用struts2 框架,需在前面加上以下内容 <%@taglib prefix="s" uri="/struts-tags" %> ...

  2. Python核心编程笔记(类)

    Python并不强求你以面向对象的方式编程(与Java不同) # coding=utf8 class FooClass(object): version = 0.1 def __init__(self ...

  3. Android MultiDex兼容包怎么使用?

    在Android系统中安装应用的时候,需要对Dex进行优化,但由于其处理工具DexOpt的限制,导致其id的数目不能够超过65536个.而MultiDex兼容包的出现,就很好的解决了这个问题,它可以配 ...

  4. 淘宝开放平台TOP测试环境

    沙箱测试环境 淘宝沙箱环境是淘宝开放平台(TOP)提供给独立软件开发商(ISV)的测试环境.数据完全独立,大部分API已经部署到该环境中供ISV进行API的功能测试,对与APP的调用量无限制,但获取大 ...

  5. 安卓开发, 遇到WebView不能加载静态网页, WebView显示 "net::ERR_PROXY_CONNECTON_FAILED"

    http://blog.csdn.net/zhouchangshi/article/details/44454695 Android开发中遇到网络连接问题, 要找WebView中显示一个静态的网页, ...

  6. sql 读取本地txt文件批量插入数据库

    --导入 INSERT INTO [netmonsdb].[dbo].[keywordlist]([keyword]) SELECT * FROM OPENROWSET( BULK 'D:/xmsys ...

  7. 【转】兼容iOS 10 资料整理

    1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到UserN ...

  8. DataGridView隔行显示不同的颜色

      如果该dataGridView是跟数据库绑定的,则可以触发DataBindingComplete事件:  1private   void   dataGridView1_DataBindingCo ...

  9. 机器学习技法--学习笔记04--Soft SVM

    背景 之前所讨论的SVM都是非常严格的hard版本,必须要求每个点都被正确的区分开.但是,实际情况时很少出现这种情况的,因为噪声数据时无法避免的.所以,需要在hard SVM上添加容错机制,使得可以容 ...

  10. Andriod调用http请求

    // 新建HttpPost对象 HttpPost httpPost = new HttpPost( "http://180.153.1.1:8080/mybankGateway/gatewa ...