Skip to content

Instantly share code, notes, and snippets.

@xpqz
Last active February 5, 2022 20:02
Show Gist options
  • Select an option

  • Save xpqz/c19ca3ef97b5437761cad3925ba5388c to your computer and use it in GitHub Desktop.

Select an option

Save xpqz/c19ca3ef97b5437761cad3925ba5388c to your computer and use it in GitHub Desktop.
Manacher's algorithm for finding the longest palindromic substring in Dyalog APL (⎕IO←0)
resmanacher str;S;c;r;radii;i;ir
c r radii0 0 (0S'|','|',str)
:for i :in S
radii[i]{r>i:radii[(2×c)-i]r-i0}
:trap 3  Catch index errors if the below goes out of bounds
:while =/S[i(+,-)1+iradii]
radii[i]+1
:endwhile
:endtrap
:if r<iri+iradii
c ri ir
:endif
:endfor
cradiir/radii
resS[c(+(+1+-)-)r]~'|'
@xpqz
Copy link
Author

xpqz commented Feb 4, 2022

Passable tradfn implementation of Manacher's algorithm (https://en.wikipedia.org/wiki/Longest_palindromic_substring)

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