Suffix Trie Construction

Posted LilyLiya

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Suffix Trie Construction相关的知识,希望对你有一定的参考价值。

refer to: https://www.algoexpert.io/questions/Suffix%20Trie%20Construction

Problem statement

 

 Example

 

 Analysis

step 1

 

 step 2

 

 step 3

 

 Time/ Space Complexity

 

 Code

class SuffixTrie:
    def __init__(self, string):
        self.root = {}
        self.endSymbol = "*"
        self.populateSuffixTrieFrom(string)

    # creation function
    def populateSuffixTrieFrom(self, string):
        for i in range(len(string)):
            self.insertSubstringStartingAt(i, string)

    def insertSubstringStartingAt(self, i, string):
        node = self.root
        for j in range(i, len(string)):
            letter = string[j]
            if letter not in node:
                node[letter] = {} # create an empty hashmap
            node = node[letter] # update the currect node
        node[self.endSymbol] = True # after iterate one whole string, add * at the end
        
    #search function
    def contains(self, string):
        node = self.root
        for letter in string:
            if letter not in node:
                return False
            node = node[letter]# if letter in node, keep down traversing the suffix tree
        return self.endSymbol in node # avoid some cases that a string has not been traversed totally 
        
        

 

 

以上是关于Suffix Trie Construction的主要内容,如果未能解决你的问题,请参考以下文章

Suffix Tree(后缀树)

浅谈Suffix Automaton(后缀自动机)

Trie树-字典树

《算法竞赛进阶指南》0x48可持久化数据结构 可持久化Trie

All knowledge of OI

the construction method (构造方法)