精華區beta Marginalman 關於我們 聯絡資訊
勝肥肥寫個trie還寫的一坨了 哀 今天又是漬漬的一天 def replaceWords(self, dictionary: List[str], sentence: str) -> str: class Node: def __init__(self): self.child = [None for _ in range(26)] self.isEnd = False root = Node() # build for v in dictionary: cur = root for c in v: if cur.child[ord(c)-ord('a')] is None: cur.child[ord(c)-ord('a')] = Node() cur = cur.child[ord(c)-ord('a')] else: cur = cur.child[ord(c)-ord('a')] cur.isEnd = True # inference words = sentence.split(' ') ans = "" for w in words: cur = root s_cur = "" for c in w: cur = cur.child[ord(c)-ord('a')] if cur is None: s_cur = w break else: s_cur += c if cur.isEnd == True: break ans += s_cur ans += " " return ans[:-1] -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.228.146.144 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1717774796.A.BCF.html
sustainer123: trie多寫幾題就背下來怎麼刻惹 06/07 23:42
JIWP: 剩我不會trie了 06/07 23:46