Z algorithm for pattern preprocessing