Maximal palindromes at all centers #
A word w of length n has 2 * n + 1 centers c = 0, …, 2 * n: an odd center c is the
letter at position c / 2, and an even center c is the gap before position c / 2 (with
c = 2 * n the gap after the last letter). A factor w.extract a b has center a + b, so
w.extract (c / 2 - k) ((c + 1) / 2 + k) is the factor with radius k about c: its length is
2 * k + c % 2. Palindromes are Mathlib's List.Palindrome.
radius w c is the maximal radius of a palindromic factor about c. This is the all-centers
formulation of Apostolico, Breslauer and Galil, Section 1: their
even palindrome of radius R centered at position k (1-indexed, letters S[k - R .. k + R - 1])
has center c = 2 * (k - 1) here, and their odd palindrome of radius R centered on position k
(letters S[k - R .. k + R]) has center c = 2 * k - 1; the maximal radii agree. The extra center
c = 2 * n always has radius 0.
Every palindromic factor is determined by the radius at its center
(palindrome_extract_iff_le_radius), so the longest palindromic factor (longestPalindrome) and
the palindromic prefixes (palindrome_take_iff) are read off the radii. Computing all radii in
linear time is Manacher's algorithm (CslibFrontier.Algorithms.Lean.Palindrome.Manacher); its key
step is the mirror lemma min_radius_mirror.
Main definitions #
Cslib.Algorithms.Lean.Palindrome.IsPalRadius:w.extract (c / 2 - k) ((c + 1) / 2 + k)is a palindromic factor ofw.Cslib.Algorithms.Lean.Palindrome.radius: the maximal palindromic radius about centerc.Cslib.Algorithms.Lean.Palindrome.maxPalindrome: the maximal palindromic factor aboutc.Cslib.Algorithms.Lean.Palindrome.bestCenter,longestPalindrome: a center maximizing2 * radius w c + c % 2and its maximal palindrome, a longest palindromic factor ofw.
Main results #
Cslib.Algorithms.Lean.Palindrome.isPalRadius_iff_le_radius: the palindromic radii aboutcare exactly0, …, radius w c(for centersc ≤ 2 * w.length).Cslib.Algorithms.Lean.Palindrome.palindrome_extract_iff_le_radius: fora ≤ b ≤ w.length,w.extract a bis a palindrome iffb - a ≤ 2 * radius w (a + b) + (a + b) % 2.Cslib.Algorithms.Lean.Palindrome.isPalRadius_succ_iff: a palindromic radiuskwith room on both sides extends tok + 1iff the two letters just outside it agree.Cslib.Algorithms.Lean.Palindrome.min_radius_mirror: the mirror lemma. If the maximal palindrome aboutCends atR, then forc ≥ Cthe radii aboutcand about2 * C - cagree once truncated atR - (c + 1) / 2.Cslib.Algorithms.Lean.Palindrome.length_le_longestPalindrome,palindrome_longestPalindrome,longestPalindrome_isInfix:longestPalindrome wis a longest palindromic factor ofw.Cslib.Algorithms.Lean.Palindrome.palindrome_take_iff:w.take kis a palindrome iffradius w k = k / 2.
References #
Palindromic radii #
The maximal palindromic radius about center c of w (0 when c > 2 * w.length).
The extension step: a palindromic radius k about c with room on both sides extends to
k + 1 iff the two letters just outside its factor agree.
The palindromic radii about a center c ≤ 2 * w.length are exactly 0, …, radius w c.
Palindromic factors are characterized by the radius at their center: for a ≤ b ≤ w.length,
w.extract a b is a palindrome iff b - a ≤ 2 * radius w (a + b) + (a + b) % 2.
Mirror lemma (the step of Manacher's algorithm). If the maximal palindrome about center
C ends at R = (C + 1) / 2 + radius w C, then for every center c ≥ C the radii about c and
about its mirror image 2 * C - c, truncated at the distance R - (c + 1) / 2 from the right end
of that palindrome, agree.
Longest palindromic factors and palindromic prefixes #
The maximal palindromic factor about center c of w (empty when c > 2 * w.length).
Every palindromic factor of w is at most as long as the maximal palindrome about its
center.
A center of w whose maximal palindrome is longest.
bestCenter w is always one of the 2 * w.length + 1 centers 0, …, 2 * w.length.
A longest palindromic factor of w: the maximal palindrome about bestCenter w.
longestPalindrome w is at least as long as every palindromic factor of w.
Palindromic prefixes are read off the radii: for k ≤ w.length, the prefix w.take k is a
palindrome iff the maximal palindrome about center k reaches the start of w.