結果

問題 No.3503 Brackets Stack Query 2
コンテスト
ユーザー dayo ZOI
提出日時 2026-04-18 18:11:51
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 2,354 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,688 ms
コンパイル使用メモリ 185,668 KB
実行使用メモリ 117,504 KB
最終ジャッジ日時 2026-04-18 18:12:07
合計ジャッジ時間 13,846 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 6 WA * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function 'Node* Node::get_child(char)':
main.cpp:36:5: warning: control reaches end of non-void function [-Wreturn-type]
   36 |     }
      |     ^

ソースコード

diff #
raw source code

#include <string>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
using ll = long long;

#define rep(i, n) for(int i=0; i<n; ++i)

struct Node {
    char c;
    Node* p;
    int status = -1;
    array<Node*, 3> child = {nullptr, nullptr, nullptr};
    Node(Node* p, char c): p(p), c(c) {}

    Node* get_child(char c) {
        if(c == '(') {
            if(child[0] == nullptr) {
                child[0] = new Node(this, '(');
            }
            return child[0];
        }
        if(c == '|') {
            if(child[1] == nullptr) {
                child[1] = new Node(this, '|');
            }
            return child[1];
        }
        if(c == ')') {
            if(child[2] == nullptr) {
                child[2] = new Node(this, ')');
            }
            return child[2];
        }
    }
};

int main() {
    Node* root = new Node(nullptr, '$');
    Node* current = root;
    ll q;
    cin >> q;
    vector<char> query(q);
    rep(i, q) {
        ll type;
        cin >> type;
        if(type == 1) {
            cin >> query[i];
            current = current->get_child(query[i]);
        }
        else {
            query[i] = ' ';
            current = current->p;
        }
    }
    stack<char> st;
    st.push('(');
    auto f = [&](auto&& f, Node* v) -> void {
        char need = st.top();
        if(need == v->c) {
            if(need == '(') {
                st.pop();
                st.push('|');
            }
            if(need == '|') {
                st.pop();
                st.push(')');
            }
            if(need == ')') {
                st.pop();
            }
        } else {
            st.push('$');
        }
        v->status = st.size();
        if(st.empty()) st.push('(');
        rep(i, 3) if(v->child[i] != nullptr) f(f, v->child[i]);
        if(st.top() == '(' && st.size() == 1) st.pop();
        if(need == v->c && (need == '(' || need == '|')) {
            st.pop();
        }
        if(need == v->c) {
            st.push(need);
        } else {
            st.pop();
        }
    };
    root->status = 0;
    f(f, root->get_child('('));
    current = root;
    rep(i, q) {
        if(query[i] != ' ') current = current->get_child(query[i]);
        else current = current->p;
        cout << (current->status == 0 ? "Yes" : "No") << "\n";
    }

}
0