字符串搜索算法 [英] String searching algorithms

查看:171
本文介绍了字符串搜索算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于两个字符串搜索算法:KMP和后缀树,这是preferred在何种情况下?给出一些实际的例子。

For the two string searching algorithms: KMP and suffix tree, which is preferred in which cases? Give some practical examples.

推荐答案

一个后缀树是更好,如果你必须回答很多,如查询是针present在草堆?。 KMP是更好,如果你只需要搜索一个字符串在另一个单独的字符串,而不必做了很多次了。

A suffix tree is better if you will have to answer a lot of queries such as "is the needle present in the haystack?". KMP is better if you only have to search for one string in another single string, and not have to do it a lot of times.

一个后缀树是一个更一般的数据结构,所以你可以做更多的事情吧。看看你可以用它做这里。 KMP是找到一个字符串是否在另一个字符串中的子串用。

A suffix tree is a much more general data structure, so you can do a lot more with it. See what you can do with it here. KMP is useful for finding if a string is a substring in another string.

您可能也想看看其他算法,如博耶 - 穆尔,< A HREF =htt​​p://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_string_search_algorithm>拉宾,卡普甚至天真的算法,因为有一些情况(输入)在哪一个更好比其他

You might also want to check out other algorithms, such as Boyer-Moore, Rabin-Karp and even the naive algorithm, as there are situations (inputs) in which one is better than the others.

底线是:

  1. 如果你有很多像我上面提到的查询,这是值得建立一个后缀树,然后回答每个查询速度更快。
  2. 如果你需要比那些类型的查询做多,后缀树也值得建设。
  3. 如果你只在乎偶尔找到一个字符串是否是另一个字符串的子串,然后用KMP。

这篇关于字符串搜索算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆