finish the KMP algorithm