AcWing 835. Trie字符串统计
原题链接
简单
作者:
将情怀讲泛滥的恶果
,
2021-04-01 15:01:39
,
所有人可见
,
阅读 250
def insert(tries,strings,nums,idx):
p=0
for s in strings:
u = ord(s)-ord("a")
if tries[p][u]==-1:
idx+=1
tries[p][u]=idx
p=tries[p][u]
nums[p]+=1
return idx
def query(tries,strings,nums):
p=0
for s in strings:
u = ord(s)-ord("a")
if tries[p][u]>-1:
p=tries[p][u]
else:
return 0
return nums[p]
if __name__=="__main__":
n=int(input())
trie = [[-1 for x in range(0,26)] for x in range(0,100000)]
nums = [0 for x in range(0,100000)]
idx=0
while n:
opt,strings = input().split()
if opt=="I":
idx = insert(trie,strings,nums,idx)
else:
cnt = query(trie,strings,nums)
print(cnt)
n-=1