-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10679.cpp
64 lines (48 loc) · 1.03 KB
/
10679.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
64
// iagorrr ;) O(s.size()*p.size())
#include <bits/stdc++.h>
using namespace std;
vector<int> strong_borders(const string& P)
{
int m = P.size(), t = -1;
vector<int> bs(m + 1, -1);
for (int j = 1; j <= m; ++j)
{
while (t > -1 and P[t] != P[j - 1])
t = bs[t];
++t;
bs[j] = (j == m or P[t] != P[j]) ? t : bs[t];
}
return bs;
}
int KMP(const string& S, const string& P)
{
int n = S.size(), m = P.size(), i = 0, j = 0, occ = 0;
vector<int> bs = strong_borders(P);
while (i <= n - m)
{
while (j < m and P[j] == S[i + j])
++j;
if (j == m) ++occ;
int shift = j - bs[j];
i += shift;
j = max(0, j - shift);
}
return occ;
}
int main(){
int T;
cin >> T;
while(T--){
string s;
cin >> s;
int t;
cin >> t;
while(t--){
string p;
cin >> p;
cout << (KMP(s, p) != 0 ? 'y' : 'n') << endl;
}
}
return 0;
}
// Accepted.