AcWing 835. Trie字符串统计(python3)
原题链接
简单
作者:
顾_9
,
2025-01-15 22:45:04
,
所有人可见
,
阅读 1
N = 100010
q = [[0 for _ in range(26)] for _ in range(N)]
cnt = [0] * N
idx = 0
def insert(s: str):
global idx
n = len(s)
p = 0
for i in range(n):
k = ord(s[i]) - ord('a')
if q[p][k] == 0:
q[p][k] = (idx + 1)
idx += 1
p = q[p][k]
cnt[p] += 1
def query(s: str):
n = len(s)
p = 0
for i in range(n):
k = ord(s[i]) - ord('a')
if q[p][k] == 0:
return 0
p = q[p][k]
return cnt[p]
if __name__ == '__main__':
n = int(input())
for _ in range(n):
s = input().split()
if s[0] == 'I':
insert(s[1])
elif s[0] == 'Q':
print(query(s[1]))