Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

string - Understanding Knuth-Morris-Pratt Algorithm

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 1 2.

I think I understand how the table is constructed but, I dont understand how to shift once mismatch has occurred. Seems like we dont even use the table when shifting?

when do we use the table?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...