Understanding Knuth-Morris-Pratt Algorithm

后端 未结 4 496
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 11:05

Can someone explain this to me? I\'ve been reading about it and it still is hard to follow.

text : ababdbaababa
pattern: ababa

table for ababa is -1 0 0

4条回答
  •  误落风尘
    2020-12-13 11:54

    Here I have briefly described computing the prefix function and shifting through the text here.

    enter image description here

    For further information: Knuth–Morris–Pratt string search algorithm

    Shifting through the text :

    Text:     ABC ABCDAB ABCDABCDABDE
    Pattern : ABCDABD
    

    Scenario 1 - There is/are some matching character/s in Pattern and Text.
    e.g 1: In here there are 3 matching characters.

    enter image description here

    Get the value from table for 3 characters. (index 2, ABC) i.e 0 Therefore shift = 3 - 0 i.e 3

    enter image description here

    e.g 2: In here there are 6 matching characters.

    enter image description here

    Get the value from table for 6 characters. (index 5, ABCDAB) i.e 2 Therefore shift = 6 - 2 i.e 4

    enter image description here

    Scenario 2 - If there is no matching characters then shift by one.

提交回复
热议问题