Skip to content

Commit 838cee9

Browse files
authored
Merge pull request #196 from AlgorithmWithGod/khj20006
[20250303] BOJ / P3 / 전설 / 권혁준
2 parents 80e6689 + 7c7d337 commit 838cee9

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
```cpp
2+
3+
#include <iostream>
4+
#include <vector>
5+
#include <algorithm>
6+
#include <map>
7+
using namespace std;
8+
9+
vector<bool> poss(2000);
10+
11+
struct Node{
12+
map<int, Node *> next;
13+
bool last;
14+
Node(){
15+
this->last = false;
16+
}
17+
};
18+
19+
struct Trie{
20+
Node *root;
21+
Trie(){
22+
root = new Node();
23+
}
24+
void insert(string &s){
25+
Node *now = root;
26+
for(int i=0;i<s.size();i++){
27+
int idx = s[i] - 'a';
28+
bool isLast = i==s.size()-1;
29+
if(!now->next[idx]) now->next[idx] = new Node();
30+
now = now->next[idx];
31+
}
32+
now->last = true;
33+
34+
}
35+
bool find(string &s, bool reverse){
36+
Node *now = root;
37+
int id = reverse ? s.size() : 0;
38+
for(int i=0;i<s.size()-1;i++) {
39+
int idx = s[i] - 'a';
40+
if(now->next.find(idx) == now->next.end()) return false;
41+
now = now->next[idx];
42+
if(reverse) id--;
43+
else id++;
44+
45+
if(reverse && poss[id] && now->last) return true;
46+
poss[id] = now->last;
47+
}
48+
return false;
49+
}
50+
};
51+
52+
int main(){
53+
cin.tie(0)->sync_with_stdio(0);
54+
55+
Trie *colors = new Trie();
56+
Trie *names = new Trie();
57+
int N, M, Q;
58+
cin>>N>>M;
59+
for(int i=0;i<N;i++) {
60+
string s;
61+
cin>>s;
62+
colors->insert(s);
63+
}
64+
for(int i=0;i<M;i++){
65+
string s;
66+
cin>>s;
67+
reverse(s.begin(), s.end());
68+
names->insert(s);
69+
}
70+
71+
for(cin>>Q;Q--;){
72+
string s;
73+
cin>>s;
74+
poss = vector<bool>(s.size());
75+
colors->find(s, 0);
76+
reverse(s.begin(), s.end());
77+
bool res = names->find(s, 1);
78+
cout<<(res ? "Yes\n" : "No\n");
79+
}
80+
81+
}
82+
83+
```

0 commit comments

Comments
 (0)