結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー t98slider
提出日時 2022-10-15 14:10:02
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 548 ms / 3,000 ms
コード長 1,614 bytes
コンパイル時間 1,610 ms
コンパイル使用メモリ 174,448 KB
実行使用メモリ 115,860 KB
最終ジャッジ日時 2024-06-26 19:47:16
合計ジャッジ時間 10,646 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 70
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

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.emplace_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.emplace_back(Node());
            }
            cur = tree[cur][c - 'a'];
        }
        tree[cur].sz++;
    }
    bool dfs(int v, int d, string &s){
        if(d == s.size())return tree[v].sz >= 1 ? true : false;
        int c0 = s[d] - 'a';
        if(tree[v][c0] != -1 && dfs(tree[v][c0], d + 1, s))return true;
        else{
            if(d + 1 >= s.size())return false;
            int c1 = s[d + 1] - 'a';
            if(tree[v][c1] == -1)return false;
            int cur = tree[tree[v][c1]][c0];
            if(cur == -1)return false;
            for(int i = d + 2; i < s.size(); i++){
                cur = tree[cur][s[i] - 'a'];
                if(cur == -1)return false;
            }
            return tree[cur].sz ? true : false;
        }
        return false;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int Q;
    cin >> Q;
    Trie trie;
    string s;
    while(Q--){
        cin >> s;
        cout << (trie.dfs(0, 0, s) ? "Yes" : "No") << '\n';
        trie.insert(s);
    }
}
0