-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.py
More file actions
55 lines (50 loc) · 1.22 KB
/
string.py
File metadata and controls
55 lines (50 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# KMP Failure Function
def get_pi(p):
m = len(p)
pi = [0] * m
j = 0
for i in range(1, m):
while j > 0 and p[i] != p[j]:
j = pi[j - 1]
if p[i] == p[j]:
j += 1
pi[i] = j
return pi
# KMP Search
def kmp(s, p):
ans = []
pi = get_pi(p)
n, m = len(s), len(p)
j = 0
for i in range(n):
while j > 0 and s[i] != p[j]:
j = pi[j - 1]
if s[i] == p[j]:
if j == m - 1:
ans.append(i - m + 1)
j = pi[j]
else:
j += 1
return ans
# Trie
class TrieNode:
def __init__(self):
self.children = {}
self.is_end = False
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word):
node = self.root
for char in word:
if char not in node.children:
node.children[char] = TrieNode()
node = node.children[char]
node.is_end = True
def search(self, word):
node = self.root
for char in word:
if char not in node.children:
return False
node = node.children[char]
return node.is_end