結果
問題 | No.2102 [Cherry Alpha *] Conditional Reflection |
ユーザー |
|
提出日時 | 2022-10-14 22:33:44 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,442 ms / 3,000 ms |
コード長 | 2,094 bytes |
コンパイル時間 | 1,654 ms |
コンパイル使用メモリ | 172,576 KB |
実行使用メモリ | 115,736 KB |
最終ジャッジ日時 | 2024-06-26 15:58:35 |
合計ジャッジ時間 | 36,388 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 70 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Trie{ struct Node { std::array<int,26> child; int sz; Node(void) :sz(0) { child.fill(-1); } const int& operator[](int k) const { return child[k]; } int& operator[](int k) { return child[k]; } }; int num; vector<Node> tree; Trie() : num(0) { tree.push_back(Node()); } void insert(const string &s){ int cur=0; for(auto c:s){ if(tree[cur][c-'a']==-1){ tree[cur][c-'a']=++num; tree.push_back(Node()); } cur=tree[cur][c-'a']; } tree[cur].sz++; } int size(const string &s){ int cur=0; for(auto c:s){ if(tree[cur][c-'a']==-1)return 0; cur=tree[cur][c-'a']; } return tree[cur].sz; } int size(int node_num){ return (node_num==-1?0:tree[node_num].sz); } int search(string &s){ function<bool(int,int,int)> dfs = [&](int v, int f, int d){ if(d == s.size()){ return (tree[v].sz >= 1 ? true : false); } int c0 = s[d] - 'a'; for(int i = 0; i < 26; i++){ if(tree[v][i] == -1)continue; if(c0 != i){ if(f == 1)continue; if(d + 1 >= s.size())continue; int c1 = s[d + 1] - 'a'; if(tree[v][c1] == -1)continue; int nxt = tree[tree[v][c1]][c0]; if(nxt == -1)continue; if(dfs(nxt, 1, d + 2))return true; }else{ if(dfs(tree[v][i], f, d + 1))return true; } } return false; }; return dfs(0, 0, 0); } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); Trie trie; int q; cin >> q; string s; while(q--){ cin >> s; cout << (trie.search(s) ? "Yes" : "No") << '\n'; trie.insert(s); } }