Skip to content

Instantly share code, notes, and snippets.

@xpqz
Last active June 25, 2021 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xpqz/1916eecdd39699f3411a87fb67fefc04 to your computer and use it in GitHub Desktop.
Save xpqz/1916eecdd39699f3411a87fb67fefc04 to your computer and use it in GitHub Desktop.
Knuth-Morris-Pratt prefix (⎕IO←0)
∇ P←prefix s;m;i;j
m←≢s
P←0⍴⍨m
j←0
:for i :in 1+⍳¯1+m
:while j≥0
:if s[j]=s[i]
:leave
:endif
:if (j-1)≥0
j←P[j-1]
:else
j←¯1
:endif
:endwhile
j+←1
P[i]←j
:endfor
@xpqz
Copy link
Author

xpqz commented Jun 25, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment