codeforces MUH and Cube Walls
题意:给定两个序列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的更多相关文章
- 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 & % ...
- Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!
D. MUH and Cube Walls 说实话,这题看懂题意后秒出思路,和顺波说了一下是KMP,后来过了一会确定了思路他开始写我中途接了个电话,回来kaungbin模板一板子上去直接A了. 题意: ...
- D - MUH and Cube Walls
D. MUH and Cube Walls Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant ...
- [codeforces471D]MUH and Cube Walls
[codeforces471D]MUH and Cube Walls 试题描述 Polar bears Menshykov and Uslada from the zoo of St. Petersb ...
- 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 ...
- Codeforces 471 D MUH and Cube Walls
题目大意 Description 给你一个字符集合,你从其中找出一些字符串出来. 希望你找出来的这些字符串的最长公共前缀*字符串的总个数最大化. Input 第一行给出数字N.N在[2,1000000 ...
- CodeForces–471D--MUH and Cube Walls(KMP)
Time limit 2000 ms Memory limit 262144 kB Polar bears Menshykov and Uslada from the zoo of ...
- MUH and Cube Walls
Codeforces Round #269 (Div. 2) D:http://codeforces.com/problemset/problem/471/D 题意:给定两个序列a ,b, 如果在a中 ...
- CF471D MUH and Cube Walls
Link 一句话题意: 给两堵墙.问 \(a\) 墙中与 \(b\) 墙顶部形状相同的区间有多少个. 这生草翻译不想多说了. 我们先来转化一下问题.对于一堵墙他的向下延伸的高度,我们是不用管的. 我们 ...
随机推荐
- javascript实例——时间日期篇(包含5个实例)
本来想在网上找一些js实例来练练手,结果发现一本书<突破JavaScript编程实例五十讲>,看了下内容还不错,就下了下来: 后面又下了该书籍的源码,一看才发现这本书编的日期是2002年的 ...
- php读取大文件的方法
1.使用file 函数直接读取 $starttime = microtime_float(); ini_set('memory_limit','-1'); $file = "testfile ...
- [原创]IBM BLM模型思维导图
[原创]IBM BLM模型思维导图 IBM业务领先模型 http://wenku.baidu.com/view/1d1d247af242336c1eb95e3b.html?from=search
- SQL Developer 4.0 启动报错“unable to create an instance of the java virtual machine located at path”
安装了Oracle之后,第一件事情就是想想怎么去连接,进而操作.SQL Developer是官方提供的强大工具,个人看来也是第一选择. 目前官网提供的最新版是4.0.1.14.48,下载下来之后,就跃 ...
- NPOI 添加行
IRow sourceRow = sheet1.GetRow(); ; i < dt.Rows.Count-; i++) { IRow row = sheet1.CreateRow( + i); ...
- HTTP状态码(HTTP Status Code)及常用场景
常见的状态码: HTTP: Status 200 – 服务器成功返回网页HTTP: Status 3xx - 表示要完成请求,需要进一步操作. 通常,这些状态代码用来重定向HTTP: Status 4 ...
- 浏览器 CSS Hack 收集
所谓的Hack就是只有特定浏览器才能识别这段hack代码.Hack 不是什么好东西,除非没有办法,我们尽量还是不要用着玩意. 下面是各个浏览器的CSS Hack 列表. Firefox 浏览器 @-m ...
- 简单的freemarker解析测试
本文是一个很简单很基础的Freemarker模板解析测试类,复杂的也是在此基础上添加一些代码优化而来,懂得基础流程后就能融会贯通了 POM: <dependency> <groupI ...
- chrome start.js报错
是由 chrome 插件 “电脑管家广告过滤” 引起的 并且,在用户电脑上还出现了这个插件拦截正常请求的情况 如果同时报以下错误: Uncaught TypeError: Cannot read pr ...
- cmd窗口编码方式的修改
cmd默认的编码是采用GBK regedit HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe\CodePage 十进制:936 GB ...