Uses a precomputed failure function (pi table) to avoid re-examining characters that already matched.