简体   繁体   中英

Confused with kmp algorithm

Can anyone please explain to me... That will kmp algorithm work when the pattern that we are matching does not start with the same letter as the main string... Eg. String :- abcdbcx Pattern :- bc If yes then please explain how.

If the first letter of the string doesn't match the first letter of the pattern, then it moves to the 2nd letter of the string and compares that to the first letter of the pattern. When there's no initial matching, it acts just like a regular string search.

The difference is when one or more letters of the string match the initial letters of the pattern and then a mismatch is found. In that case the position in the main string is advanced 1 from where it already is and it uses the pre-built table to determine where in the pattern it should reset to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM