ADCBCADCBA <- haystack
CBA <- needle
If the needle doesn't match, shift the needle over so the next character in haystack, B, lines up with the last B in needle. Repeat until done.So here we shift by 2 so the Bs line up.
ADCBCADCBA
CBA
No match. Shift by 1 so the As line up. ADCBCADCBA
CBA
No match. The next character is D. Since there are no Ds in needle to line up, shift needle past the D. ADCBCADCBA
CBA
Match!The main point is the amount to shift depends only on the next character so it can be precomputed and stored in an array (qsBc). You always shift by 1 to line up an A, 2 to line up a B, and 3 to line up a C. Anything else you shift by 4.