結果

問題 No.3667 Prefix Count Queries
ユーザー 👑 loop0919
提出日時 2026-08-31 22:09:15
言語 PyPy3
(8.0.0 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 750 ms / 2,000 ms
+ 980µs
コード長 865 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 159 ms
コンパイル使用メモリ 64,500 KB
実行使用メモリ 153,896 KB
最終ジャッジ日時 2026-10-23 04:51:47
合計ジャッジ時間 19,012 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

N = int(input())
A = [[*input()] for _ in range(N)]

trie = [dict()]
count = [0]

for a in A:
    curr = 0
    count[0] += 1

    for c in a:
        if c in trie[curr]:
            to = trie[curr][c]
        else:
            to = len(trie)
            trie.append(dict())
            count.append(0)

        trie[curr][c] = to
        count[to] += 1
        curr = to

Q = int(input())
curr = 0
stack = [0]
ans = N

for _ in range(Q):
    cmd, *query = input().split()
    cmd = int(cmd)

    if cmd == 1:
        x = query[0]

        if x in trie[curr]:
            to = trie[curr][x]
        else:
            to = len(trie)
            trie.append(dict())
            count.append(0)

        trie[curr][x] = to
        stack.append(to)
        curr = to

    elif cmd == 2:
        stack.pop()
        curr = stack[-1]

    else:
        print(count[curr])
0