Last active
June 25, 2021 17:48
-
-
Save xpqz/1916eecdd39699f3411a87fb67fefc04 to your computer and use it in GitHub Desktop.
Knuth-Morris-Pratt prefix (⎕IO←0)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
∇ 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 | |
∇ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://brilliant.org/wiki/knuth-morris-pratt-algorithm/