classTrie: def__init__(self): self.root = TrieNode() defadd(self, s, idx): cur = self.root for c in s: cur = cur.next[c] cur.end.append(idx) defsearch(self, prefix): """ 搜索 prefix 为前缀的单词数量 """ p = self.root for c in prefix: if c in p.next: p = p.next[c] else: returnset() returnself.dfs(p, set()) defdfs(self, root, ans): """ 对字典树结点进行深度优先搜索 """ for idx in root.end: ans.add(idx) for c, node in root.next.items(): self.dfs(node, ans) return ans
classWordFilter:
def__init__(self, words: List[str]): self.pref, self.suff = Trie(), Trie() for idx, w inenumerate(words): self.pref.add(w, idx) self.suff.add(reversed(w), idx)
deff(self, pref: str, suff: str) -> int: p = self.pref.search(pref) s = self.suff.search(reversed(suff)) ans = p & s if ans: returnmax(ans) return -1
时间复杂度: 建立字典树 \(O(n)\),
\(n\) 表示 words
的长度,搜索 \(O(m)\), \(m\)
表示字典树的大小,最坏情况下要搜索整颗字典树,相当于退化到暴力枚举求解了
classTrie: def__init__(self): self.root = TrieNode() defadd(self, s, idx): cur = self.root for c in s: cur = cur.next[c] cur.end = idx defsearch(self, prefix): """ 搜索 prefix 为前缀的单词 """ p = self.root for c in prefix: if c in p.next: p = p.next[c] else: returnset() returnself.dfs(p, set()) defdfs(self, root, ans): """ 对字典树结点进行深度优先搜索 """ if root.end != -1: ans.add(root.end) for c, node in root.next.items(): self.dfs(node, ans) return ans
classWordFilter:
def__init__(self, words: List[str]): self.pref, self.suff = Trie(), Trie() for idx, w inenumerate(words): self.pref.add(w, idx) self.suff.add(reversed(w), idx)
@cache deff(self, pref: str, suff: str) -> int: p = self.pref.search(pref) s = self.suff.search(reversed(suff)) ans = p & s if ans: returnmax(ans) return -1
时间复杂度: 建立字典树 \(O(n)\),
\(n\) 表示 words
的长度,搜索 \(O(m)\), \(m\)
表示字典树的大小,最坏情况下要搜索整颗字典树,相当于退化到暴力枚举求解了
classTrie: def__init__(self): self.root = TrieNode() defadd(self, s, idx): cur = self.root for c in s: cur = cur.next[c] cur.end = idx defsearch(self, prefix): """ 搜索 prefix 为前缀的单词 """ p = self.root for c in prefix: if c in p.next: p = p.next[c] else: returnset() returnself.dfs(p, set()) defdfs(self, root, ans): """ 对字典树结点进行深度优先搜索 """ if root.end != -1: ans.add(root.end) for c, node in root.next.items(): self.dfs(node, ans) return ans
classWordFilter:
def__init__(self, words: List[str]): self.words = words self.pref = Trie() for idx, w inenumerate(words): self.pref.add(w, idx)
classTrie: def__init__(self): self.root = TrieNode() defadd(self, s, idx): cur = self.root for c in s: cur = cur.next[c] cur.end = idx defsearch(self, prefix): """ 搜索 prefix 为前缀的单词 """ p = self.root for c in prefix: if c in p.next: p = p.next[c] else: returnset() returnself.dfs(p, set()) defdfs(self, root, ans): """ 对字典树结点进行深度优先搜索 """ if root.end != -1: ans.add(root.end) for c, node in root.next.items(): self.dfs(node, ans) return ans
classWordFilter:
def__init__(self, words: List[str]): self.words = words self.pref = Trie() for idx, w inenumerate(words): self.pref.add(w, idx) self.pref_cache = dict() self.pref_suff = dict()
deff(self, pref: str, suff: str) -> int: key = f"{pref}_{suff}" if key inself.pref_suff: returnself.pref_suff[key] if pref notinself.pref_cache: self.pref_cache[pref] = sorted(self.pref.search(pref), reverse=True) ans = self.pref_cache[pref] for idx in ans: ifself.words[idx].endswith(suff): self.pref_suff[key] = idx return idx self.pref_suff[key] = -1 return -1
时间复杂度: 建立字典树 \(O(n)\),
\(n\) 表示 words
的长度,搜索 \(O(m)\), \(m\)
表示字典树的大小,最坏情况下要搜索整颗字典树,相当于退化到暴力枚举求解了
classTrie: def__init__(self): self.root = TrieNode() defadd(self, s, idx): cur = self.root for c in s: cur = cur.next[c] cur.string.append(idx) defsearch(self, prefix): """ 搜索 prefix 为前缀的单词 """ p = self.root for c in prefix: if c in p.next: p = p.next[c] else: return [] return p.string
classWordFilter:
def__init__(self, words: List[str]): self.words = words self.pref = Trie() for idx, w inenumerate(words): self.pref.add(w, idx) self.pref_suff = dict()