1 Star 4 Fork 0

Sshwy/code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
471d.cpp 1.05 KB
一键复制 编辑 原始数据 按行查看 历史
Sshwy 提交于 4年前 . format files
// by Yao
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
namespace RA {
int rnd(int p) { return 1ll * rand() * rand() % p; }
int rnd(int L, int R) { return rnd(R - L + 1) + L; }
} // namespace RA
const int N = 2e5 + 5;
int n, m;
int a[N], b[N], nex[N];
void getNext(int *s, int ls) {
nex[1] = 0;
int j = 0;
FOR(i, 2, ls) {
while (j && s[i] != s[j + 1]) j = nex[j];
if (s[i] == s[j + 1])
nex[i] = ++j;
else
nex[i] = 0;
}
}
int main() {
scanf("%d%d", &n, &m);
if (m == 1) return printf("%d\n", n), 0;
FOR(i, 1, n) scanf("%d", &a[i]);
FOR(i, 1, m) scanf("%d", &b[i]);
FOR(i, 1, n) a[i] -= a[i + 1];
FOR(i, 1, m) b[i] -= b[i + 1];
--n, --m;
b[m + 1] = -2e9;
getNext(b, m);
int j = 0;
int ans = 0;
FOR(i, 1, n) {
while (j && a[i] != b[j + 1]) j = nex[j];
if (a[i] == b[j + 1])
++j;
else
j = 0;
if (j == m) ++ans;
}
printf("%d\n", ans);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sshwy/code.git
git@gitee.com:sshwy/code.git
sshwy
code
code
master

搜索帮助