AcWing 835. Trie字符串统计python
原题链接
简单
作者:
柠檬茶去冰
,
2024-12-09 18:29:14
,
所有人可见
,
阅读 1
python 代码
def insert(char):
global idx
p = 0
for i in char:
u = ord(i) - 97 #u存的是值
if not a[p][u]:
idx += 1
a[p][u] = idx
p = a[p][u] #走到的深度
cnt[p] += 1
def query(char):
p = 0
for i in char:
u = ord(i) - 97
if not a[p][u]:
return 0
p = a[p][u]
return cnt[p]
n = int(input())
a = [[0] * 26 for i in range(10010)]
cnt = [0] * 10010
idx = 0 # 下标是0即是根节点,又是空节点
for _ in range(n):
op = input().split()
if op[0] == 'I':
insert(op[-1])
else:
print(query(op[-1]))