From 7c7d3379f704c34876592c6d30a7660faa30c5c7 Mon Sep 17 00:00:00 2001 From: oncsr Date: Mon, 3 Mar 2025 20:44:23 +0900 Subject: [PATCH] =?UTF-8?q?[20250303]=20BOJ=20/=20P3=20/=20=EC=A0=84?= =?UTF-8?q?=EC=84=A4=20/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../03 BOJ P3 \354\240\204\354\204\244.md" | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 "khj20006/202503/03 BOJ P3 \354\240\204\354\204\244.md" diff --git "a/khj20006/202503/03 BOJ P3 \354\240\204\354\204\244.md" "b/khj20006/202503/03 BOJ P3 \354\240\204\354\204\244.md" new file mode 100644 index 00000000..a6f3b4dd --- /dev/null +++ "b/khj20006/202503/03 BOJ P3 \354\240\204\354\204\244.md" @@ -0,0 +1,83 @@ +```cpp + +#include +#include +#include +#include +using namespace std; + +vector poss(2000); + +struct Node{ + map next; + bool last; + Node(){ + this->last = false; + } +}; + +struct Trie{ + Node *root; + Trie(){ + root = new Node(); + } + void insert(string &s){ + Node *now = root; + for(int i=0;inext[idx]) now->next[idx] = new Node(); + now = now->next[idx]; + } + now->last = true; + + } + bool find(string &s, bool reverse){ + Node *now = root; + int id = reverse ? s.size() : 0; + for(int i=0;inext.find(idx) == now->next.end()) return false; + now = now->next[idx]; + if(reverse) id--; + else id++; + + if(reverse && poss[id] && now->last) return true; + poss[id] = now->last; + } + return false; + } +}; + +int main(){ + cin.tie(0)->sync_with_stdio(0); + + Trie *colors = new Trie(); + Trie *names = new Trie(); + int N, M, Q; + cin>>N>>M; + for(int i=0;i>s; + colors->insert(s); + } + for(int i=0;i>s; + reverse(s.begin(), s.end()); + names->insert(s); + } + + for(cin>>Q;Q--;){ + string s; + cin>>s; + poss = vector(s.size()); + colors->find(s, 0); + reverse(s.begin(), s.end()); + bool res = names->find(s, 1); + cout<<(res ? "Yes\n" : "No\n"); + } + +} + +```