-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ16172.cpp
63 lines (48 loc) · 947 Bytes
/
BOJ16172.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
55
56
57
58
59
60
61
62
63
//BOJ16172
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define MAXI 1234567
int N, M;
string S, W;
int fail[MAXI] = { 0, };
void getfail(string Q) {
for (int i = 1, j = 0; i < Q.length(); ++i) {
while (j > 0 && Q[i] != Q[j]) j = fail[j - 1];
if (Q[i] == Q[j]) {
++j;
fail[i] = j;
}
}
}
vector<int> KMP(string S, string W) {
vector<int> ret;
for (int i = 0, j = 0; i < S.length(); ++i) {
while (j > 0 && S[i] != W[j]) {
j = fail[j - 1];
}
if (S[i] == W[j]) {
if (j == W.length() - 1) {
ret.push_back(i - W.length() + 1);
j = fail[j];
}
else ++j;
}
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> S >> W;
string proc;
for (int i = 0; i < S.length(); ++i) {
if (S[i] < 'A') continue;
proc += S[i];
}
getfail(W);
vector<int> ans = KMP(proc, W);
if (ans.size()) printf("1\n");
else printf("0\n");
return 0;
}