結果

問題 No.3667 Prefix Count Queries
ユーザー Rino-program
提出日時 2026-08-15 19:59:39
言語 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
結果
AC  
実行時間 38 ms / 2,000 ms
+ 393µs
コード長 1,616 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,389 ms
コンパイル使用メモリ 186,676 KB
実行使用メモリ 14,220 KB
最終ジャッジ日時 2026-09-01 00:50:52
合計ジャッジ時間 7,776 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using namespace std;

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

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

    // 接頭辞のハッシュ出現回数を記録するマップ
    unordered_map<unsigned long long, int> hash_count;
    const unsigned long long BASE = 10007; // 固定のBase(狙い撃ちされやすい)

    // 空文字列(ハッシュ値 0)の初期件数は N
    hash_count[0] = N;

    for (int i = 0; i < N; ++i) {
        string A;
        cin >> A;
        unsigned long long h = 0;
        for (char c : A) {
            h = h * BASE + c;
            hash_count[h]++;
        }
    }

    int Q;
    cin >> Q;

    // S のハッシュ値の履歴(末尾削除に対応するためスタックで管理)
    vector<unsigned long long> hash_history;
    hash_history.push_back(0); // 初期状態(空文字列)

    while (Q--) {
        int type;
        cin >> type;
        if (type == 1) {
            char x;
            cin >> x;
            unsigned long long next_hash = hash_history.back() * BASE + x;
            hash_history.push_back(next_hash);
        } else if (type == 2) {
            hash_history.pop_back();
        } else if (type == 3) {
            unsigned long long current_hash = hash_history.back();
            auto it = hash_count.find(current_hash);
            if (it != hash_count.end()) {
                cout << it->second << "\n";
            } else {
                cout << 0 << "\n";
            }
        }
    }

    return 0;
}
0