-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinding Patterns.cpp
More file actions
executable file
·126 lines (118 loc) · 2.3 KB
/
Copy pathFinding Patterns.cpp
File metadata and controls
executable file
·126 lines (118 loc) · 2.3 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
class AhoCorasick {
public:
AhoCorasick() {
for (int i = 0; i < 26; i++) v[i] = nullptr;
end = false;
vis = false;
fail = nullptr;
parent = nullptr;
}
void insert(string &w, int id, vector<AhoCorasick *> &ending) {
AhoCorasick *t = this;
for (auto c : w) {
if (t->v[c - 'a'] == nullptr) {
t->v[c - 'a'] = new AhoCorasick;
t->v[c - 'a']->parent = t;
t->v[c - 'a']->c = c;
}
t = t->v[c - 'a'];
}
t->end = true;
ending[id] = t;
}
void build() {
queue<AhoCorasick *> q;
this->fail = this;
for (int i = 0; i < 26; i++) {
if (this->v[i] != nullptr) {
q.push(this->v[i]);
}
}
while (!q.empty()) {
auto x = q.front();
q.pop();
for (int i = 0; i < 26; i++) {
if (x->v[i] != nullptr) {
q.push(x->v[i]);
}
}
if (x->parent == this) {
x->fail = this;
} else {
auto tmp = x->parent->fail;
auto c = x->c;
while (tmp != this && tmp->v[c - 'a'] == nullptr) tmp = tmp->fail;
if (tmp->v[c - 'a']) tmp = tmp->v[c - 'a'];
x->fail = tmp;
}
}
}
void search(string &s) {
auto tmp = this;
for (auto c : s) {
while (tmp != this && tmp->v[c - 'a'] == nullptr) tmp = tmp->fail;
if (tmp->v[c - 'a']) tmp = tmp->v[c - 'a'];
tmp->vis = true;
}
}
void find_visited() {
queue<AhoCorasick *> q;
for (int i = 0; i < 26; i++) {
if (this->v[i] != nullptr) {
q.push(this->v[i]);
}
}
while (!q.empty()) {
auto x = q.front();
q.pop();
auto y = x;
while (y->vis && y->fail != this && !y->fail->vis) {
y->fail->vis = true;
y = y->fail;
}
for (int i = 0; i < 26; i++) {
if (x->v[i] != nullptr) {
q.push(x->v[i]);
}
}
}
}
bool is_visited() {
return vis;
}
private:
AhoCorasick *v[26];
bool end, vis;
AhoCorasick *fail, *parent;
char c;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int n;
cin >> n;
AhoCorasick *root = new AhoCorasick;
vector<AhoCorasick *> ending(n);
for (int i = 0; i < n; i++) {
string w;
cin >> w;
root->insert(w, i, ending);
}
root->build();
root->search(s);
root->find_visited();
for (int i = 0; i < n; i++) {
if (ending[i]->is_visited()) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
return 0;
}