资源 · 指南或文章
Rabin Karp Algorithm
介绍 Rabin-Karp 算法,这是一种基于哈希的字符串搜索,页面将其归功于 Karp 和 Rabin,并解释为什么哈希匹配后仍需直接比较。
审核日期
打开原站: Rabin Karp Algorithm (在新标签页中打开原网站)练习这个主题以 技术 / 编程 模式打开练习面试官,然后在那里选择题目。
添加到路线图
把这个资源放进你正在跟进的路线图。它会作为你自己的补充资源显示;路线图的步骤不会改变,也不计入你已记录的步骤。
来源内容语言:英语
In computer science, the Rabin–Karp algorithm or Karp–Rabin algorithm is a string searching algorithm created by Richard M. Karp and Michael O. Rabin (1987) that uses hashing to find any one of a set of pattern strings in a text. The Rabin–Karp algorithm seeks to speed up the testing of equality of the pattern to the substrings in the text by using a hash function. A hash function is a function which converts every string into a numeric value, called its hash value; for example, we might have hash('hello') = 5. The algorithm exploits the fact that if two strings are equal, their hash values are also equal. Thus, string matching is reduced (almost) to computing the hash value of the search pattern and then looking for substrings of the input string with that hash value. However, there are two problems with this approach. First, because there are so many different strings and so few hash values, some differing strings will have the same hash value. If the hash values match, the pattern and the substring may not match; consequently, the potential match of search pattern and the substring must be confirmed by comparing them; that comparison can take a long time for long substrings…
PrepKit 概览
本概览由 PrepKit 编辑根据源代码仓库撰写并审核,并非资源本身的文字。
- 适合人群
- 来源未说明适用级别
- 形式
- 参考资料
涵盖内容
- 解释如何把模式匹配转化为比较哈希值,以及冲突为什么需要一个确认步骤
- 指出滚动哈希让相邻子串的哈希计算保持低成本,并说明示例使用的是多项式哈希而不是 Rabin 指纹
- 给出平均和最好情况时间 O(n + m)、空间 O(p),最坏情况 O(n * m)
- 把抄袭检测描述为多模式匹配的用例
使用建议
这是 PrepKit 的学习建议,并非来源的说法。
实现一个多项式滚动哈希,让它在文本上滑动,并记录每次哈希命中以及直接比较是否确认了匹配。之后你应能解释最坏情况从何而来。
范围与局限
只讲概念:没有滚动哈希的具体演算,没有代码讲解,也没有练习。更深入的细节留给维基百科和 YouTube 链接。
获取方式与形式
GitHub 上 trekhleb/javascript-algorithms(MIT)中的 README。英文,免费阅读。
PrepKit 于 2026年10月2日 审核。 资源来源提交: 85293e3e2b88
本概览的依据
- src/algorithms/string/rabin-karp/README.md
收录此条目的页面
订阅 PrepKit 通讯
可选。使用 PrepKit 并不需要订阅。