zoukankan      html  css  js  c++  java
  • 数据结构之字符串

    字符串

    • 实现一个字符集,只包含a~z这26个英文字母的Trie树
    • 实现朴素的字符串匹配算法

    class Trie:
        # word_end = -1
     
        def __init__(self):
            """
            Initialize your data structure here.
            """
            self.root = {}
            self.word_end = -1
     
        def insert(self, word):
            """
            Inserts a word into the trie.
            :type word: str
            :rtype: void
            """
            curNode = self.root
            for c in word:
                if not c in curNode:
                    curNode[c] = {}
                curNode = curNode[c]
              
            curNode[self.word_end] = True
     
        def search(self, word):
            """
            Returns if the word is in the trie.
            :type word: str
            :rtype: bool
            """
            curNode = self.root
            for c in word:
                if not c in curNode:
                    return False
                curNode = curNode[c]
                
            # Doesn't end here
            if self.word_end not in curNode:
                return False
            
            return True
     
        def startsWith(self, prefix):
            """
            Returns if there is any word in the trie that starts with the given prefix.
            :type prefix: str
            :rtype: bool
            """
            curNode = self.root
            for c in prefix:
                if not c in curNode:
                    return False
                curNode = curNode[c]
            
            return True
     
     
    # Your Trie object will be instantiated and called as such:
    # obj = Trie()
    # obj.insert(word)
    # param_2 = obj.search(word)
    # param_3 = obj.startsWith(prefix)
    朴素的字符串匹配算法

    算法基本思想:

        将搜索词整个后移一位,再从头逐个比较。这样做虽然可行,但是效率很差,因为你要把"搜索位置"移到已经比较过的位置,重比一遍
        遇字符不等时将模式串p右移一个字符,再次从p0(重置j = 0 后)开始比较
        最坏情况是每趟比较都在最后出现不等,最多比较n-m+1 趟,总比较次数为m*(n-m+1),所以算法时间复杂性为O(m*n)

    def nmatching(t, p):
        i, j = 0, 0
        n, m = len(t), len(p)
        while i < n and j < m:
            if t[i] == p[j]:
                i, j = i+1, j+1
            else:
                i, j = i-j+1, 0        #i-j+1是关键,遇字符不等时将模式串t右移一个字符
        if j == m:                     #找到一个匹配,返回索引值
            return i-j
        return -1                       #未找到,返回-1
     
        # else:
        #     return -1
     
    t = 'aabaabaabab'
    p = 'baab'
    print(nmatching(t,p))

  • 相关阅读:
    Linux的inode的理解
    linux中ctrl+z和ctrl+c的区别
    linux后台运行和关闭、查看后台任务
    解决Could not get lock /var/cache/apt/archives/lock
    Spring Boot 2.1.5 正式发布,1.5.x 即将结束使命!
    【免费】某平台16980元编程课程资料下载,仅此1次
    秒杀系统架构分析与实战,一文带你搞懂秒杀架构!
    阿里数据库大牛的 MySQL 学习指南!
    Intellij IDEA 撸码最头大的问题。。
    Java 中的 SPI 机制是什么鬼?高级 Java 必须掌握!
  • 原文地址:https://www.cnblogs.com/hrnn/p/13347277.html
Copyright © 2011-2022 走看看