MCPcopy Create free account
hub / github.com/acm-clan/algorithm-stone / compute_prefix_function

Function compute_prefix_function

templates/kmp.py:5–22  ·  view source on GitHub ↗
(p, back=False)

Source from the content-addressed store, hash-verified

3import string
4
5def compute_prefix_function(p, back=False):
6 n = len(p)
7 next = np.zeros(n, dtype=int)
8 k = -1
9 j = 0
10 next[0] = -1
11 while j < n-1:
12 if k == -1 or p[j] == p[k]:
13 k+=1
14 j+=1
15 next[j] = k
16 if back:
17 print("next[%s]=%s"%(str(j), str(k)))
18 else:
19 if back:
20 print("back:", p[j], p[k], k, "->", next[k])
21 k = next[k]
22 return next
23
24def check_next(next):
25 last = -2

Callers 2

runFunction · 0.70
run2Function · 0.70

Calls 1

printFunction · 0.85

Tested by

no test coverage detected