結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー t98slidert98slider
提出日時 2022-10-15 13:25:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 457 ms / 3,000 ms
コード長 1,973 bytes
コンパイル時間 1,700 ms
コンパイル使用メモリ 176,488 KB
実行使用メモリ 115,872 KB
最終ジャッジ日時 2024-06-26 19:43:38
合計ジャッジ時間 14,440 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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.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);
    }
    bool dfs(int v, bool f, int d, string &s){
        if(d == s.size())return (tree[v].sz >= 1 ? true : false);
        int c0 = s[d] - 'a';
        if(f){
            return tree[v][c0] == -1 ? false : dfs(tree[v][c0], true, d + 1, s);
        }
        for(int i = 0; i < 26; i++){
            if(tree[v][i] == -1)continue;
            if(c0 != i){
                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, true, d + 2, s))return true;
            }else{
                if(dfs(tree[v][i], f, d + 1, s))return true;
            }
        }
        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, false, 0, s) ? "Yes" : "No") << '\n';
        trie.insert(s);
    }
}
0