結果

問題 No.3667 Prefix Count Queries
ユーザー Rino-program
提出日時 2026-08-15 20:02:31
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 2,202 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,005 ms
コンパイル使用メモリ 169,460 KB
実行使用メモリ 50,688 KB
最終ジャッジ日時 2026-08-31 20:30:35
合計ジャッジ時間 3,950 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4 WA * 1
other AC * 17 WA * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct Node {
    int count = 0;
    Node* children[26] = {nullptr};
    Node* parent = nullptr;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    if (!(cin >> N)) return 0;

    Node* root = new Node();

    // Trie 木の構築
    for (int i = 0; i < N; ++i) {
        string A;
        cin >> A;
        Node* curr = root;
        curr->count++;
        for (char c : A) {
            int idx = c - 'a';
            if (!curr->children[idx]) {
                curr->children[idx] = new Node();
                curr->children[idx]->parent = curr;
            }
            curr = curr->children[idx];
            curr->count++;
        }
    }

    int Q;
    cin >> Q;

    Node* curr = root;
    int invalid_depth = 0; // 一致するノードが存在しなくなってからの文字数

    while (Q--) {
        int type;
        cin >> type;

        if (type == 1) {
            char x;
            cin >> x;
            if (invalid_depth > 0) {
                // すでに一致しない状態なら深さのカウントだけ増やす(省略処理)
                invalid_depth++;
            } else {
                int idx = x - 'a';
                if (curr && curr->children[idx]) {
                    curr = curr->children[idx];
                } else {
                    // 遷移先がないため Trie の範囲外へ出る
                    invalid_depth = 1;
                    curr = nullptr; // ノード参照を破棄してしまう
                }
            }
        } else if (type == 2) {
            if (invalid_depth > 0) {
                invalid_depth--;
                // invalid_depth が 0 に戻っても、curr が nullptr のまま復元されない!
            } else {
                if (curr && curr->parent) {
                    curr = curr->parent;
                }
            }
        } else if (type == 3) {
            if (invalid_depth > 0 || curr == nullptr) {
                cout << 0 << "\n";
            } else {
                cout << curr->count << "\n";
            }
        }
    }

    return 0;
}
0