結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー t98slider
提出日時 2022-10-15 14:05:05
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,660 bytes
コンパイル時間 1,793 ms
コンパイル使用メモリ 171,280 KB
実行使用メモリ 115,608 KB
最終ジャッジ日時 2024-06-26 19:46:56
合計ジャッジ時間 11,049 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25 WA * 45
権限があれば一括ダウンロードができます

ソースコード

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 search(string &s){
        int cur = 0, d = 0;
        for(int i = 0; i < s.size(); i++){
            if(tree[cur][s[i] - 'a'] == -1){
                if(i + 1 == s.size())return false;
                swap(s[i], s[i + 1]);
                bool ans = true;
                for(int j = i; j < s.size(); j++){
                    cur = tree[cur][s[j] - 'a'];
                    if(cur == -1){
                        ans = false;
                        break;
                    }
                }
                swap(s[i], s[i + 1]);
                return ans && tree[cur].sz ? true : false;
            }
            cur = tree[cur][s[i] - 'a'];
        }
        return tree[cur].sz ? true : 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.search(s) ? "Yes" : "No") << '\n';
        trie.insert(s);
    }
}
0