• 欢迎光临~

字典树Trie模板

开发技术 开发技术 2022-08-20 次浏览
Python版本
class Trie:
    def __init__(self):
        self.children = defaultdict(Trie)
        self.word = ""
        self.is_word = False
    def insert(self, word):
        cur = self
        for w in word:
            cur = cur.children[w]
        cur.is_word = True
        cur.word = word
    def search(self, word):
        cur = self
        for w in word:
            if w not in cur.children:
                return False
            cur = cur.children[w]
        return cur.is_word
程序员灯塔
转载请注明原文链接:字典树Trie模板
喜欢 (0)