3.8.9 · D1 · HinglishString Algorithms

FoundationsPalindrome algorithms — Manacher's algorithm

1,927 words9 min read↑ Read in English

3.8.9 · D1 · Coding › String Algorithms › Palindrome algorithms — Manacher's algorithm

Yeh page assume karta hai ki aapne kuch bhi nahi dekha. Hum har letter, bracket, aur idea ko build karte hain jis par parent note (Palindrome algorithms — Manacher's algorithm (index 3.8.9)) rely karta hai, un ke dependency order mein. End tak aap us note ki har line bina ruke padh sakte hain.


1. Ek string, aur uske positions

Figure — Palindrome algorithms — Manacher's algorithm

Yeh topic ko kyun chahiye? Har baad ka idea — center, radius, mirror — ek statement about indices hai. Agar indices shaky hain, toh unke upar kuch bhi khada nahi rahega.


2. Palindrome actually hota kya hai

Jo picture important hai woh hai ek center line ke baare mein symmetry: string ko aadha fold karo aur dono halves exactly ek doosre ke upar land karti hain.

Figure — Palindrome algorithms — Manacher's algorithm

Topic ko yeh kyun chahiye: poora algorithm hai "har possible center ke liye, mirror kitni door tak hold karta hai?" Aapko pehle crystal clear hona chahiye ki center kya hota hai aur uske do kinds hote hain.


3. Center aur radius

Figure — Palindrome algorithms — Manacher's algorithm

Topic ko yeh kyun chahiye: algorithm jo array build karta hai, , exactly "center par radius" hai. Radius woh quantity hai jo twins ke beech cleanly mirror hoti hai — length nahi karti.


4. # transformation (odd + even → ek shape)

Figure — Palindrome algorithms — Manacher's algorithm

Topic ko yeh kyun chahiye: yeh do messy cases ko ek uniform loop mein convert karta hai. Yeh reference code mein sabse zyada reuse hone wala idea hai (t = '#' + '#'.join(s) + '#').


5. Ek center ke baare mein index ko mirror karna

Topic ko yeh kyun chahiye: symmetry ka matlab hai "point apne twin jaisa dikhta hai." Twin ke already-computed radius ko copy karne ke liye, aapko jaanna hoga ki twin kaunsa index hai. Woh index hai.


6. Jaana-maana window: aur

Topic ko yeh kyun chahiye: min(P[2C-i], R-i) — algorithm ka dil — dono (wall) aur (twin dhundhne ke liye) use karta hai. nahi, toh reuse nahi, toh linear time nahi.


7. Tool: min, aur wall par cap kyun karte hain

Topic ko yeh kyun chahiye: yeh single min hi woh cheez hai jo initial guess ko fresh count ki jagah banata hai.


8. Amortized cost — expand loop cheap kyun hai

Topic ko yeh kyun chahiye: yeh poora "" ka justification hai. Iske bina aap nahi bata sakte ki Manacher kyun Expand Around Center ke worst case ko beat karta hai.


Yeh topic ko kaise feed karte hain

string s and index from 0

palindrome = symmetry about a center

center and radius

hash transform to t makes all odd

radius array P equals length in s

mirror index i prime = 2C minus i

window C and R the known reach

min of twin and wall gives cheap guess

expand past wall pushes R right

amortized O of n

Manacher algorithm

Related destinations jab aap ready hon: Longest Palindromic Substring, Expand Around Center, aur cousins jo same "copy from a known prefix" trick reuse karte hain — Z-Algorithm, KMP failure function, aur Palindromic Tree (Eertree).


Equipment checklist

Kya aap s = "abcd" ke liye s[2] ki value aur bata sakte hain?
aur .
abba odd- ya even-length palindrome hai, aur uska center kahan hai?
Even; center dono bs ke beech mein hai (koi single center box nahi).
racecar palindrome ka apne center e ke baare mein radius kya hai?
Radius (har side mein rac); length .
abc ko # trick se transform karo.
(length ).
Agar mein ek center ka radius hai, toh mein palindrome kitni lambi hai?
Length (radius in equals length in ).
Center , current point . Mirror index kya hai?
.
Window mein hai, aur aap par hain; twin promise karta hai. Aap kaunsa initial radius lete hain?
— wall par capped.
Manacher kyun hai aur kyun nahi?
Kyunki expansion sirf ko rightward push karti hai, aur poore total mein at most baar move karta hai (amortized linear).