結果

問題 No.3667 Prefix Count Queries
ユーザー Rino-program
提出日時 2026-08-15 19:55:55
言語 PyPy3
(7.3.23)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 269 ms / 2,000 ms
+ 812µs
コード長 1,061 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 224 ms
コンパイル使用メモリ 95,956 KB
実行使用メモリ 165,316 KB
最終ジャッジ日時 2026-09-01 00:50:37
合計ジャッジ時間 10,639 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import defaultdict

N = int(input())
# Trie木の構築
Trie = {"cnt": [N], "to": [{}]}
for i in range(N):
    S = input()
    curr = 0
    for c in S:
        if c not in Trie["to"][curr]:
            Trie["to"][curr][c] = len(Trie["cnt"])
            Trie["cnt"].append(0)
            Trie["to"].append({})
        curr = Trie["to"][curr][c]
        Trie["cnt"][curr] += 1

Q = int(input())
# クエリの処理
curr = 0
history = [0]
ans = []
for _ in range(Q):
    ipt = input().split()
    if ipt[0] == "1":
        # 文字列の追加
        X = ipt[1]
        if curr != -1 and X in Trie["to"][curr]:
            curr = Trie["to"][curr][X]
        else:
            curr = -1
        history.append(curr)
    elif ipt[0] == "2":
        # 文字列の削除
        history.pop()
        curr = history[-1]
    else:
        # 現在のノードに対応する文字列の数を出力
        if curr == -1:
            ans.append(0)
        else:
            ans.append(Trie["cnt"][curr])

# 結果の出力
for a in ans:
    print(a)
0