結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー t98slidert98slider
提出日時 2022-10-14 22:33:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,464 ms / 3,000 ms
コード長 2,094 bytes
コンパイル時間 1,954 ms
コンパイル使用メモリ 169,864 KB
実行使用メモリ 117,000 KB
最終ジャッジ日時 2023-09-08 22:44:38
合計ジャッジ時間 35,233 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 313 ms
30,972 KB
testcase_03 AC 438 ms
58,568 KB
testcase_04 AC 316 ms
30,820 KB
testcase_05 AC 143 ms
11,808 KB
testcase_06 AC 323 ms
31,308 KB
testcase_07 AC 270 ms
113,824 KB
testcase_08 AC 26 ms
4,376 KB
testcase_09 AC 406 ms
58,632 KB
testcase_10 AC 100 ms
10,876 KB
testcase_11 AC 150 ms
17,980 KB
testcase_12 AC 275 ms
31,992 KB
testcase_13 AC 520 ms
58,964 KB
testcase_14 AC 221 ms
17,952 KB
testcase_15 AC 345 ms
30,884 KB
testcase_16 AC 599 ms
58,928 KB
testcase_17 AC 176 ms
59,400 KB
testcase_18 AC 393 ms
115,300 KB
testcase_19 AC 167 ms
17,196 KB
testcase_20 AC 555 ms
60,112 KB
testcase_21 AC 48 ms
4,804 KB
testcase_22 AC 667 ms
59,348 KB
testcase_23 AC 665 ms
60,048 KB
testcase_24 AC 684 ms
60,056 KB
testcase_25 AC 686 ms
60,424 KB
testcase_26 AC 677 ms
58,524 KB
testcase_27 AC 672 ms
58,664 KB
testcase_28 AC 664 ms
59,696 KB
testcase_29 AC 668 ms
59,656 KB
testcase_30 AC 670 ms
58,632 KB
testcase_31 AC 669 ms
58,660 KB
testcase_32 AC 671 ms
58,676 KB
testcase_33 AC 672 ms
58,984 KB
testcase_34 AC 663 ms
58,592 KB
testcase_35 AC 665 ms
60,012 KB
testcase_36 AC 669 ms
60,120 KB
testcase_37 AC 669 ms
60,404 KB
testcase_38 AC 673 ms
59,912 KB
testcase_39 AC 683 ms
58,828 KB
testcase_40 AC 666 ms
58,608 KB
testcase_41 AC 672 ms
59,656 KB
testcase_42 AC 739 ms
58,956 KB
testcase_43 AC 740 ms
59,456 KB
testcase_44 AC 733 ms
59,392 KB
testcase_45 AC 733 ms
59,888 KB
testcase_46 AC 745 ms
59,804 KB
testcase_47 AC 392 ms
113,664 KB
testcase_48 AC 384 ms
114,528 KB
testcase_49 AC 387 ms
113,776 KB
testcase_50 AC 392 ms
113,972 KB
testcase_51 AC 390 ms
114,684 KB
testcase_52 AC 100 ms
116,224 KB
testcase_53 AC 101 ms
117,000 KB
testcase_54 AC 100 ms
115,872 KB
testcase_55 AC 104 ms
113,696 KB
testcase_56 AC 106 ms
114,524 KB
testcase_57 AC 108 ms
114,480 KB
testcase_58 AC 111 ms
111,356 KB
testcase_59 AC 878 ms
60,212 KB
testcase_60 AC 19 ms
4,384 KB
testcase_61 AC 109 ms
114,088 KB
testcase_62 AC 8 ms
4,808 KB
testcase_63 AC 285 ms
4,392 KB
testcase_64 AC 105 ms
4,380 KB
testcase_65 AC 112 ms
4,812 KB
testcase_66 AC 1,464 ms
114,100 KB
testcase_67 AC 41 ms
4,380 KB
testcase_68 AC 29 ms
4,888 KB
testcase_69 AC 35 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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);
    }
}
0