classTrie:# 构建字典树def__init__(self,words):
self.d ={}for word in words:
t = self.d
for w in word:if w notin t:
t[w]={}
t = t[w]
t['end']= word
# print(self.d)defsearch(self,s):
t = self.d
res =[]for w in s:if w notin t:break
t = t[w]if'end'in t:
res.append(t['end'])return res
classSolution:defmultiSearch(self, big:str, smalls: List[str])-> List[List[int]]:"""
{'i': {'s': {'end': 'is'}, 'end': 'i'},
'p': {'p': {'i': {'end': 'ppi'}}},
'h': {'i': {'end': 'hi'}},
's': {'i': {'s': {'end': 'sis'}},
's': {'i': {'p': {'p': {'i': {'end': 'ssippi'}}}}}}}
"""
trie = Trie(smalls)
hit = collections.defaultdict(list)for i inrange(len(big)):
matchs = trie.search(big[i:])for word in matchs:
hit[word].append(i)
res =[]for word in smalls:
res.append(hit[word])return res