-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkmp.py
37 lines (34 loc) · 892 Bytes
/
kmp.py
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
# python3
import sys
def ComputePrefixFunction(P):
S=[0 for _ in range(len(P))]
S[0]=0
border=0
for i in range(1,len(P)):
while(border>0) and (P[i] !=P[border]):
border=S[border-1]
if P[i]==P[border]:
border+=1
else:
border=0
S[i]=border
return S
def find_pattern(pattern, text):
"""
Find all the occurrences of the pattern in the text
and return a list of all positions in the text
where the pattern starts in the text.
"""
# Implement this function yourself
new=pattern+'$'+text
S=ComputePrefixFunction(new)
result = []
for i in range(len(pattern), len(new)):
if S[i]==len(pattern):
result.append(i-2*len(pattern))
return result
if __name__ == '__main__':
pattern = sys.stdin.readline().strip()
text = sys.stdin.readline().strip()
result = find_pattern(pattern, text)
print(" ".join(map(str, result)))