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