implement kmp uglily