当前位置: 首页>>代码示例>>Python>>正文


Python Trie.has_prefixes方法代码示例

本文整理汇总了Python中trie.Trie.has_prefixes方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.has_prefixes方法的具体用法?Python Trie.has_prefixes怎么用?Python Trie.has_prefixes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在trie.Trie的用法示例。


在下文中一共展示了Trie.has_prefixes方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: find_compound_words

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import has_prefixes [as 别名]
def find_compound_words(words):
    """ trie + BFS + pruning
    Advantages of trie:
    1. Predictable O(k) lookup time where k is the size of the key.
    2. We can easily get all prefixes of a given word.
    Drawbacks of tries:
    1. Space-consuming, it is a trade-off between time-complexity and space\
    complexity. We can use radix-tree to get optimized space, but in \
    practice, it doesn't have a reasonable improvement and it takes more\
    time than trie.
    """
    compound_words = set([])
    trie = Trie()
    queue = collections.deque()
    prefixes_dict = {}
    for word in words:
        prefixes = trie.has_prefixes(word)
        for prefix in prefixes:
            queue.append((word, word[len(prefix) :]))
        trie.insert(word)
    while queue:
        word, suffix = queue.popleft()
        # pruning
        if word in compound_words:
            continue
        # find a compund word
        if suffix in trie:
            compound_words.add(word)
        else:
            prefixes = trie.has_prefixes(suffix)
            for prefix in prefixes:
                queue.append((word, suffix[len(prefix) :]))
    return compound_words
开发者ID:redswallow,项目名称:acm-icpc,代码行数:35,代码来源:solver.py

示例2: TestTrie

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import has_prefixes [as 别名]
class TestTrie(unittest.TestCase):
    def setUp(self):
        self.rt = Trie()
        self.rt.insert('cat')
        self.rt.insert('cats')
        self.rt.insert('dog')
        self.rt.insert('data')

    def test_insert(self):
        node = self.rt.root
        # insert 'cat'
        self.assertTrue('c' in node.children)
        # insert 'cats'
        self.assertTrue('c' == node.children['c'].letter)
        self.assertTrue('a' in node.children['c'].children)
        # insert 'dog'
        # insert 'data'
        self.assertTrue('d' in node.children)
        self.assertTrue('o' in node.children['d'].children)
        self.assertTrue('a' in node.children['d'].children)

    def test_contains(self):
        self.assertTrue('cat' in self.rt)
        self.assertTrue('cats' in self.rt)
        self.assertTrue('dog' in self.rt)
        self.assertTrue('data' in self.rt)
        self.assertFalse('ca' in self.rt)
        self.assertFalse('c' in self.rt)
        self.assertFalse('d' in self.rt)
        self.assertFalse('dogs' in self.rt)

    def test_has_prefixes(self):
        self.assertEquals(['cat'], self.rt.has_prefixes('cat'))
        self.assertEquals(['cat', 'cats'], self.rt.has_prefixes('cats'))
        self.assertEquals(['dog'], self.rt.has_prefixes('dog'))
开发者ID:redswallow,项目名称:acm-icpc,代码行数:37,代码来源:test_trie.py


注:本文中的trie.Trie.has_prefixes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。