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
|
-
|
|
-
|
-
|
!
| #module
#ifdef __hspdef__ #define global while(%1=1) %tcontinue %i0 %twhile *%i :%tbreak if %1=0 { goto *%i }
#define global wend %tcontinue *%o : %twhile goto *%o: %tbreak *%o
#endif
#defcfunc KMPsearch str p1, int p2, str p3
s = p1 : w = p3
dim num, 2
num = strlen(p1), strlen(p3)
dim t, num(1)
t = -1, 0 : i = 2 while i < num(1)
if strmid(w, i-1, 1) == strmid(w, j, 1) {
t(i) = j +1
i ++ : j ++
} else : if j > 0 {
j = t(j)
} else {
t(i) = 0 : i ++
}
wend
m = p2 : i = 0
while (m + i) < num(0)
if strmid(w, i, 1) == strmid(s, m +i, 1) { i ++ } else { m += i -t(i) : if i > 0 { i = t(i) } }
if i == num(1) : return m
await 0
wend
return -1 #global
|