templates/manacher.cpp
compilable
$cat templates/manacher
Manacher's Algorithm
Find all palindromic substrings in O(n) time using linear expansion with symmetry reuse.
#manacher#palindrome#string#linear
Log in to track progress, save custom versions, and organize into collections.Log In
$cat source_code/
cpp
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;
vector<int> manacher(const string &s) {
string t = "#";
for (char c : s) { t += c; t += '#'; }
int n = t.size();
vector<int> p(n);
int l = 0, r = -1;
for (int i = 0; i < n; i++) {
int k = (i > r) ? 1 : min(p[l + r - i], r - i + 1);
while (i - k >= 0 && i + k < n && t[i - k] == t[i + k])
k++;
p[i] = k--;
if (i + k > r) { l = i - k; r = i + k; }
}
return p;
}18 linesutf-8
$cat explanation_notes.md
notes_viewer --renderedmarkdown (math enabled)
Manacher's Algorithm
Finds all palindromic substrings in by exploiting the symmetry of already-discovered palindromes.
How It Works
# between every character: aba → #a#b#a#. Now every palindrome (odd and even) is odd-length in the transformed string- If : reuse the mirror position's radius as starting point
- Expand outward while characters match
- Update if current palindrome extends further right