Z-algorithm