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 Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one on top of the other. They defined multiple towers standing in a line as a wall. A wall can consist of towers of different heights.
Horace was the first to finish making his wall. He called his wall an elephant. The wall consists of w towers. The bears also finished making their wall but they didn't give it a name. Their wall consists of n towers. Horace looked at the bears' tower and wondered: in how many parts of the wall can he "see an elephant"? He can "see an elephant" on a segment of w contiguous towers if the heights of the towers on the segment match as a sequence the heights of the towers in Horace's wall. In order to see as many elephants as possible, Horace can raise and lower his wall. He even can lower the wall below the ground level (see the pictures to the samples for clarification).
Your task is to count the number of segments where Horace can "see an elephant".
Input
The first line contains two integers n and w (1 ≤ n, w ≤ 2·105) — the number of towers in the bears' and the elephant's walls correspondingly. The second line contains n integers ai (1 ≤ ai ≤ 109) — the heights of the towers in the bears' wall. The third line contains w integers bi (1 ≤ bi ≤ 109) — the heights of the towers in the elephant's wall.
Output
Print the number of segments in the bears' wall where Horace can "see an elephant".
Example
13 5
2 4 5 5 4 3 2 2 2 3 3 2 1
3 4 4 3 2
2
Note
The picture to the left shows Horace's wall from the sample, the picture to the right shows the bears' wall. The segments where Horace can "see an elephant" are in gray.
题目大意:给定一个序列A,它的某一个子串通过每个元素加上一个x,使得这个子串和序列B相同,问有多少个这样的开始位置不同的子串。
如果没有加上x这个条件就是KMP模板,那么只能找个不会变化的量来进行KMP,这个量就是后一项和前一项的差(没有后一项的的话不管)。特殊地,当m = 1时,结果为n。
Code
/**
* codeforces
* Problem#471D
* Accepted
* Time:46ms
* Memory:3720k
*/
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<ctime>
#include<set>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef bool boolean;
#define INF 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#ifndef WIN32
#define AUTO "%lld"
#else
#define AUTO "%I64d"
#endif
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-');
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} int n, m;
int *S, *T;
int *f; inline void init() {
readInteger(n);
readInteger(m);
S = new int[(const int)n];
T = new int[(const int)m];
int last = ;
for(int i = , a; i <= n; i++) {
readInteger(a);
if(i > ) {
S[i - ] = a - last;
}
last = a;
}
for(int i = , a; i <= m; i++) {
readInteger(a);
if(i > ) {
T[i - ] = a - last;
}
last = a;
}
n--, m--;
} inline void getFail() {
f = new int[(const int)(m + )];
f[] = f[] = ;
for(int i = , j; i < m; i++) {
j = f[i];
while(j > && T[i] != T[j]) j = f[j];
f[i + ] = (T[i] == T[j]) ? (j + ) : ();
}
} int res = ;
inline void kmp() {
getFail();
for(int i = , j = ; i < n; i++) {
while(j > && S[i] != T[j]) j = f[j];
if(S[i] == T[j] && j < m) j++;
if(j == m) res++, j = f[j];
}
} inline void solve() {
if(m == ) {
printf("%d", n + );
return;
}
kmp();
printf("%d", res);
} int main() {
init();
solve();
return ;
}
CodeForces 471D MUH and Cube Walls -KMP的更多相关文章
- 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)
Time limit 2000 ms Memory limit 262144 kB Polar bears Menshykov and Uslada from the zoo of ...
- 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串中出现过!最后输出 ...
- Codeforces 471 D MUH and Cube Walls
题目大意 Description 给你一个字符集合,你从其中找出一些字符串出来. 希望你找出来的这些字符串的最长公共前缀*字符串的总个数最大化. Input 第一行给出数字N.N在[2,1000000 ...
- 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\) 墙顶部形状相同的区间有多少个. 这生草翻译不想多说了. 我们先来转化一下问题.对于一堵墙他的向下延伸的高度,我们是不用管的. 我们 ...
随机推荐
- The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:
运行环境: Intellij idea 14 在改了项目名称. 运行时候出现了 The APR based Apache Tomcat Native library which allows opti ...
- AOP 详解
1. 需求:统计方法执行的性能情况(来源:<精通Spring 4.x>) // 性能监视类 PerformanceMonitor package com.noodles.proxy; pu ...
- 【python-opencv】22-直方图
直方图目录: 22.1 直方图的计算,绘制与分析 22.1.1 统计直方图 22.1.2 绘制直方图 22.1.3 使用掩膜(遮罩) 22.2 直方图均衡化 22.2.1 OpenCV中的直方图均衡化 ...
- ssh stricthostkeychecking=0
SSH 公钥检查是一个重要的安全机制,可以防范中间人劫持等黑客攻击. 但是在特定情况下,严格的 SSH 公钥检查会破坏一些依赖 SSH 协议的自动化任务,就需要一种手段能够绕过 SSH 的公钥检查. ...
- 【剑指offer】替换空格
一.题目: 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 二.思路: Python代码,先 ...
- windows server r2 之如何设置共享文件夹访问不需要输入用户名和密码
第一步: 打开guest账号.单击桌面“开始”按钮,找到“控制面板”并打开,选择“用户帐户”并单击就会弹出一个窗口,继续单击下方的“管理其他帐户”,然后选择“Guest”,点击“启用”. 第二步: 在 ...
- 手工利用Chrome浏览器“Javascript控制台”
1.打开Chrome浏览器,输入网址:http://forum.csdn.net/SList/HTMLCSS/ 2.按下“Ctrl+Shift+J”打开“Javascript控制台”工具 3.动态引用 ...
- python接口测试中安装whl格式的requests第三方模块
下载 安装 requests第三方模块 下载:http://docs.python-requests.org/en/latest/user/install/#install 我下载是 https:// ...
- C#webBrowser使用代理服务器的方法winform
其实在C#中使用webBrowser大家应该都会了,论坛也有很多相前的例子大家可以查询一下就知道了但是像直接使用浏览器一样设置代理 的方法可能很多人还不知道吧.这个其实是调用一个Dll文件进行设置的, ...
- scipy模块