What is the worst case for KMP string search algorithm? [closed]

谁都会走 提交于 2019-12-22 06:16:34

问题


Can anyone suggest me a worst case "text string - pattern pair" for testing a KMP algorithm implementation?


回答1:


I would say a pattern like

xx........x
| n times |

and a string like

xxx.........xyx...........xy....
| n-1 times | | n-1 times |

would be one of the worst cases, but it's still O(m+n)




回答2:


You can find anything on KMP algorithm here :

KMP ALGORITHM

Quick extract :

Knuth, Morris and Pratt discovered first linear time string-matching algorithm by following a tight analysis of the naïve algorithm. Knuth-Morris-Pratt algorithm keeps the information that naïve approach wasted gathered during the scan of the text. By avoiding this waste of information, it achieves a running time of O(n + m), which is optimal in the worst case sense. That is, in the worst case Knuth-Morris-Pratt algorithm we have to examine all the characters in the text and pattern at least once.

You should be able to calrify what you understand of the algorithm and find what you need there.

hope it helps



来源:https://stackoverflow.com/questions/7839165/what-is-the-worst-case-for-kmp-string-search-algorithm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!