AcWing 142. 前缀统计python
原题链接
简单
作者:
柠檬茶去冰
,
2024-12-09 20:37:47
,
所有人可见
,
阅读 2
Trie=[[0]*26 for i in range(1000000)]
ed=[0]*1000000
idx=0
def insert(char):
global idx
p=0
for i in range(len(char)):
u=ord(char[i])-97
if not Trie[p][u]:
idx+=1
Trie[p][u]=idx
p=Trie[p][u]
ed[p]+=1
def query(char):
p=0
l=0
for i in range(len(char)):
u=ord(char[i])-97
if not Trie[p][u]:
return l
p=Trie[p][u]
l+=ed[p]
return l
n,m=map(int,input().split())
for _ in range(n):
s=input()
insert(s)
for _ in range(m):
t=input()
print(query(t))