When to use Rabin-Karp or KMP algorithms?

后端 未结 1 1758
有刺的猬
有刺的猬 2021-01-31 17:24

I have generated an string using the following alphabet. {A,C,G,T}. And my string contains more than 10000 characters. I\'m searching the following patterns in it.<

相关标签:
1条回答
  • 2021-01-31 17:44

    When you want to search for multiple patterns, typically the correct choice is to use Aho-Corasick, which is somewhat a generalization of KMP. Now in your case you are only searching for 3 patterns so it may be the case that KMP is not that much slower(at most three times), but this is the general approach.

    Rabin-Karp is easier to implement if we assume that a collision will never happen, but if the problem you have is a typical string searching KMP will be more stable no matter what input you have. However, Rabin-Karp has many other applications, where KMP is not an option.

    0 讨论(0)
提交回复
热议问题