首页 > 其他 > 详细

数据结构之字符串

时间:2020-07-20 22:17:52      阅读:70      评论:0      收藏:0      [点我收藏+]

字符串

  • 实现一个字符集,只包含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))

数据结构之字符串

原文:https://www.cnblogs.com/hrnn/p/13347277.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!