Manacher's algorithm computes the maximal palindrome at every center full
This proof shows: correctness.
Let s=s[0..n−1], m=2n+1, and T[0..m−1] the implicit string with T[2k]=# and T[2k+1]=s[k], where # is not a symbol of s. Indices are 0-based. For 0≤c≤m−1 let ρ(c) be the largest r≥0 with c−r≥0, c+r≤m−1 and T[c−r..c+r] a palindrome. Write W(c,q) for the window T[c−q..c+q].
Fact 1 (nesting). If W(c,q) is a palindrome and q≥1, then W(c,q−1) is a palindrome. So W(c,q) is a palindrome if and only if q≤ρ(c), and if q<ρ(c), then c−q−1≥0, c+q+1≤m−1 and T[c−q−1]=T[c+q+1].
Proof. Removing the first and the last symbol of a palindrome leaves a palindrome. The second claim follows, because W(c,ρ(c)) is a palindrome and contains W(c,q) for q≤ρ(c), and its symbols at distance q+1 from c are equal. □
Fact 2 (mirror). Let X=T[a..b] be a palindrome, and x↦a+b−x its mirror map. If W(x,q) lies inside [a,b], then W(a+b−x,q) is the reversal of W(x,q). So one is a palindrome if and only if the other is.
Proof. For ∣d∣≤q, T[x+d]=T[a+b−x−d] because X is a palindrome. So the window at a+b−x, read from left to right, is the window at x read from right to left. □
Lemma (mirror lemma). Let c<i<r with r=c+ρ(c), and j=2c−i. Then ρ(i)≥min(ρ(j),r−i). If ρ(j)<r−i, then ρ(i)=ρ(j). If ρ(j)>r−i, then ρ(i)=r−i.
Proof. Let L=c−ρ(c)=2c−r, so X=T[L..r] is a palindrome with mirror map x↦2c−x, which sends i to j. Since c<i<r, we have L<j<c. Let q=min(ρ(j),r−i). Then W(i,q) lies inside [i−(r−i),r]⊆[L,r], because i−(r−i)=2i−r>2c−r=L. The window W(j,q) is a palindrome by Fact 1, so by Fact 2, W(i,q) is a palindrome, and ρ(i)≥q.
Case ρ(j)<r−i. Suppose ρ(i)≥ρ(j)+1. The window W(i,ρ(j)+1) is a palindrome, and it lies inside [L,r] because ρ(j)+1≤r−i. By Fact 2, W(j,ρ(j)+1) is a palindrome, which contradicts the definition of ρ(j). So ρ(i)=ρ(j).
Case ρ(j)>r−i. Let d=r−i. If r=m−1, then i+d+1=m is outside T and ρ(i)≤d. Otherwise L≥1, because j−ρ(j)≥0 and L−1=j−d−1≥j−ρ(j). By the maximality of ρ(c), T[L−1]=T[r+1]. Now L−1=j−(d+1), and since d+1≤ρ(j), the palindrome W(j,ρ(j)) gives T[L−1]=T[j+d+1]. The position j+d+1=2c−2i+r+1 lies in [L,r], because i>c gives 2c−2i+1≤0. Its mirror in X is 2c−(j+d+1)=i−d−1, so T[j+d+1]=T[i−d−1]. Together, T[i−d−1]=T[L−1]=T[r+1]=T[i+d+1], so W(i,d+1) is not a palindrome, and ρ(i)=d by Fact 1. □
Invariant. At the start of the iteration of line L4 with index i:
(I) P[k]=ρ(k) for all k<i, and P[k]=0 for k≥i. If i=0, then c=r=0. If i≥1, then c<i, r=c+ρ(c), and r=maxk<i(k+ρ(k)).
At i=0, lines L2 and L3 make (I) true. Assume (I) at i.
Line L5. If i<r, then i≥1, so c<i<r=c+ρ(c), and 2c−i<c<i, so P[2c−i]=ρ(2c−i). Line L5 sets P[i]=min(ρ(2c−i),r−i), which is at most ρ(i) by the mirror lemma. If i≥r, P[i] stays 0. In both cases W(i,P[i]) is a palindrome and P[i]≤ρ(i).
Lines L6 and L7. The test of line L6 is "W(i,P[i]+1) lies in T and its two new ends are equal", and since W(i,P[i]) is a palindrome, this is the test "W(i,P[i]+1) is a palindrome". By Fact 1 it succeeds exactly while P[i]<ρ(i). So the loop ends with P[i]=ρ(i). It ends, because P[i] grows and is bounded by ρ(i).
Line L8. If i+ρ(i)>r, line L8 sets c=i and r=i+ρ(i); otherwise it keeps them. In both cases r=maxk≤i(k+ρ(k)) and r=c+ρ(c) with c≤i<i+1. For i=0, ρ(0)=0 and c=r=0, which is also of this form. So (I) holds at i+1.
At the end of the loop, (I) with i=m gives P=ρ, which line L9 returns.
From T to s. Fix c and let ρ=ρ(c).
Step 1: the window W(c,ρ) ends on separators. Suppose c−ρ is odd. Then c+ρ is odd too, so 1≤c−ρ and c+ρ≤2n−1, and both c−ρ−1 and c+ρ+1 are even positions inside T. They hold #, so W(c,ρ+1) is a palindrome, a contradiction. So c−ρ=2a and c+ρ=2b for integers 0≤a≤b≤n, with a+b=c and b−a=ρ.
Step 2: windows with separator ends. For 0≤a≤b≤n, the string T[2a..2b] is a palindrome if and only if s[a..b−1] is a palindrome. The mirror of 2a..2b sends even positions to even positions, where T is always #, and the odd position 2k+1, which holds s[k], to 2a+2b−2k−1=2(a+b−1−k)+1, which holds s[a+b−1−k]. So T[2a..2b] is a palindrome exactly when s[k]=s[a+b−1−k] for a≤k≤b−1, which is the definition of a palindrome for s[a..b−1].
Step 3. Each palindrome s[i..j] with i≤j+1 corresponds by Step 2 to the palindrome T[2i..2j+2] with center c=i+j+1 and radius j−i+1. So the palindromes of s with center c, that is with i+j+1=c, are the windows W(c,q) with even ends, and their lengths are the radii q≡c(mod2) with q≤ρ(c), by Fact 1. By Step 1, ρ(c) itself is such a radius. So the longest palindrome of s with center c has length P[c]=ρ(c) and is s[a..b−1] with a=(c−P[c])/2 and b=(c+P[c])/2. And for 0≤i≤j≤n−1, s[i..j] is a palindrome if and only if j−i+1≤P[i+j+1]. ■