-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path103.cpp
54 lines (45 loc) · 1.04 KB
/
103.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @file 103.cpp
* @author Macesuted ([email protected])
* @date 2022-05-01
*
* @copyright Copyright (c) 2022
*
*/
#include <bits/stdc++.h>
using namespace std;
bool mem1;
#define maxn 1000005
int nxt[maxn];
void solve(void) {
string a, b;
cin >> a >> b;
int n = a.size(), m = b.size();
nxt[0] = -1;
for (int i = 1; i < m; i++) {
nxt[i] = nxt[i - 1];
while (~nxt[i] && b[nxt[i] + 1] != b[i]) nxt[i] = nxt[nxt[i]];
nxt[i] += (b[nxt[i] + 1] == b[i]);
}
int cnt = 0;
for (int i = 0, p = -1; i < n; i++) {
while (~p && a[i] != b[p + 1]) p = nxt[p];
p += (a[i] == b[p + 1]);
if (p == m - 1) cnt++, p = nxt[p];
}
cout << cnt << endl;
return;
}
bool mem2;
int main() {
ios::sync_with_stdio(false);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif
int _ = 1;
while (_--) solve();
#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}