SHIFT DERIVE KAISE KARTE HAIN. Humne pattern index j par text char c ke saath mismatch kiya.
Hum P ko right move karna chahte hain taaki P mein rightmost c, position s+j ke neeche align ho.
Agar last(c)<j: woh occurrence j se left mein hai, toh shift =j−last(c)≥1.
Agar last(c)=−1: P mein koi c nahi, poora pattern us se aage slide karo: j−(−1)=j+1.
Agar last(c)>j: use align karna pattern ko peeche move kar dega — illegal. Hum minimum shift 1 tak clamp karte hain.
ISKO KAISE COMPUTE KARTE HAIN (shift_gs[] array jo mismatch position j se index hota hai).
Hum do helper arrays use karte hain. f ko standard "Z/border-from-the-right" preprocessing se define karo:
Maano suff[i] = P ke longest suffix ki length jo index i par khatam hoti hai aur P ka bhi suffix hai:
suff[i]=max{ℓ:P[i−ℓ+1…i]=P[m−ℓ…m−1]}.
suff se hum good-suffix shift array G[0…m] banate hain (shift jab mismatch index j par hai, yaani matched suffix length m−1−j):
Init sabhi G[⋅] ko m karo (Case 2 default, phir refine karo).
Case 2 (prefix = suffix-of-suffix): jin positions par P ka prefix border hai, wahan shifts set karo.
Case 1 (internal reoccurrence): har i ke liye jahan suff[i]=i+1 (yaani ek prefix match karta hai), ya jahan bhi P[i+1…] reoccur kare, corresponding shift record karo.
Boyer-Moore pattern ko right-to-left kyun compare karta hai?
Taaki mismatch ek matched suffix reveal kare, good-suffix rule se bade skips enable ho aur bad text char ko P mein uski rightmost copy ke saath align kar sakein.
last(c) (last-occurrence function) define karo.
Sabse bada index i jahan P[i]=c ho, ya −1 agar c∈/P.
Mismatch index j par text char c ke saath bad-character shift formula?
max(1,j−last(c)).
Bad-character shift mein max(1,⋅) kyun?
Jab last(c)≥j toh raw value ≤0 ho jaati hai (backward/zero shift) → infinite loop; kam se kam 1 tak clamp karo.
"Good suffix" kya hai?
Pehle se matched suffix P[j+1…m−1] jab index j par mismatch hoti hai.
Good-suffix rule ke do cases?
(1) Good suffix ki ek aur internal occurrence align karo jiske pehle wala char P[j] se alag ho; (2) P ka ek prefix align karo jo good suffix ke suffix ke barabar ho.
Mismatch par final BM shift?
max(bad-char shift,good-suffix shift).
Dono shifts ka max lena safe kyun hai?
Har rule ek independent valid lower bound hai safe jump ke liye; do safe jumps mein se bada bhi safe hai.
Basic BM search ka best-case aur worst-case time?
Best O(n/m) (sublinear), worst O(nm); Galil rule ise O(n) bana deta hai.
Preprocessing cost?
Bad char ke liye O(m+∣Σ∣), good suffix ke liye O(m).
Recall Feynman: 12-saal ke bachche ko samjhao
Socho ek word stamp ko ek lambi sentence par match karna hai. Letters ko left se right check karne ki jagah, tum stamp ko uske aakhiri letter se peeche ki taraf check karte ho. Jaise hi koi letter match nahi karta, tum do sawaal poochte ho: "Mere stamp mein yeh galat letter aur kahan aata hai?" (slide karo taaki woh line up ho jaaye) aur "Jo part match hua — kya woh mere stamp mein kahin pehle bhi aata hai?" (slide karo taaki woh bhi line up ho jaaye). Phir tum dono mein se bade jump se jump karte ho. Kyunki tum kabhi kabhi sentence ke poore chunks skip kar dete ho bina dekhe, yeh super fast hai.