implement KMP algorithm