class Trie:
root = {}
END = '/'
def add(self, wordtuple):
node = self.root
for c in wordtuple[0]:
node = node.setdefault(c,{})
node[self.END] = (wordtuple[1])
def find_equal(self, word):
node = self.root
for c in word:
if c not in node:
return None
node = node[c]
return node['/']
我有一棵特里树,并构造了一个嵌套字典,例如root = {'a':{'b':{'m':{'/':(4),'f':{'/':(5)}}}}}
,并且我有一个前缀字符串abm
,但是我想让所有以abm
开头的字符串都像字符串abmf
,我该怎么办?