Longest Palindrome¶
- Title:
Longest Palindrome - Judge / source:
CSES - Original URL: https://cses.fi/problemset/task/1111
- Secondary topics:
Manacher,Odd/even centers,Longest palindromic substring - Difficulty:
medium - Subtype:
Recover one longest palindromic substring from static-string palindrome radii - Status:
solved - Solution file: longestpalindrome.cpp
Why Practice This¶
This is the cleanest in-repo anchor for Manacher.
The statement asks for exactly one thing:
- the longest palindromic substring of one static string
So the real lesson is not dynamic updates, hash collision tradeoffs, or suffix machinery.
It is simply:
- why every palindrome has an odd or even center
- how
d1andd2capture the maximal radii - how one linear scan recovers the longest answer
Recognition Cue¶
Reach for Manacher when:
- the string is static
- the obvious brute force is "expand around every center"
- the task wants longest palindrome or all palindrome radii
- exactness matters, but the full problem is still just one string scan
The strongest smell here is:
- "determine the longest palindromic substring"
That is the textbook first Manacher problem.
Problem-Specific Transformation¶
Keep two arrays:
d1[i]= odd radius count at centerid2[i]= even radius count at spliti
Then:
- odd palindrome length at
iis2 * d1[i] - 1 - even palindrome length at
iis2 * d2[i]
So the statement becomes:
- compute all odd/even radii in
O(n) - scan those radii once
- recover the best substring bounds
Core Idea¶
Run two linear passes:
- odd centers
- even centers
Each pass maintains the rightmost palindrome interval found so far.
If a new center lies inside that interval, its mirror center gives a safe starting radius.
Then:
- expand beyond the known boundary if possible
- update the rightmost interval if this palindrome reaches farther
Once all radii are known, the answer is just the maximum among:
2 * d1[i] - 12 * d2[i]
with the correct left bound formulas:
- odd ->
i - d1[i] + 1 - even ->
i - d2[i]
Complexity¶
- odd pass:
O(n) - even pass:
O(n) - final scan for best bounds:
O(n)
Total:
\[
O(n)
\]
This fits easily for n <= 10^6.
Pitfalls / Judge Notes¶
- Even palindromes are centered on a split, not on a character.
d1[i]is not the full length; odd length is2 * d1[i] - 1.d2[i]already counts matched layers, so even length is2 * d2[i].- The judge accepts any longest palindrome if several exist, so there is no extra lexicographic requirement.
Reusable Pattern¶
- Topic page: Palindromes / Manacher
- Practice ladder: Palindromes ladder
- Starter template: manacher.cpp
- Notebook refresher: Palindromes hot sheet
- Compare points: String Hashing, Z-Function
- This note adds: the direct longest-substring extraction layer on top of the raw odd/even radii arrays.
Solutions¶
- Code: longestpalindrome.cpp