<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
# Rabin Karp - Rabin-Karp算法
--------
#### 問題
在文本$$ text $$中查找字符串$$ pattern $$出現的所有位置($$ text $$長度為$$ n $$,$$ pattern $$長度為$$ m $$,$$ n, m $$都是正整數且$$ n \gt m $$)。
#### 解法
從位置$$ i, j $$開始的匹配,每次成功的匹配都需要時間復雜度為$$ O(m) $$的遍歷,才能確定$$ text[i \dots i+m-1] = pattern[0 \dots m-1] = pattern $$是否成立。如果用$$ hash(i, i+m-1) $$來表示字符串$$ text[i \dots i+m-1] $$的哈希值,$$ hash(pattern) $$表示字符串$$ pattern $$的哈希值,則比較$$ hash(i, i+m-1) = hash(pattern) $$是否成功的時間復雜度為$$ O(1) $$。顯然當$$ hash(i, i+m-1) \ne hash(pattern) $$時必然有$$ text[i \dots i+m-1] \ne pattern $$。反之若哈希值相同,再用簡單匹配來確定字符串$$ text[i \dots i+m-1] = pattern $$確實為真,這樣即可找出所有成功匹配。
我們通過Rabin Fingerprint算法計算字符串的哈希值,ASCII碼中每個字符對應的數字值范圍在$$ [0 - 127] $$之間,設每讀入新的字符時舊的哈希值的擴大倍數為$$ base = 128 $$(這個$$ base $$是字符表大小的范圍)。則有:
$$
hash(0, i+1) = hash(0, i) \cdot base + text[i+1]
$$
實際我們想計算的是$$ hash(text[i \dots i+m-1]) $$,當$$ i $$右移一位時,不僅要考慮右邊界新加入的字符,還需要考慮左邊界離開的字符。一個字符從最右邊界一直移動到最左邊界,其值乘以$$ base $$共$$ m-1 $$次。因此哈希值要減去$$ base^{m-1} \cdot text[i] $$。特別注意$$ pattern $$長度為$$ 1 $$時$$ base^{m-1} = 1 $$:
$$
hash(i+1, i+m) = hash(i, i+m-1) \cdot base - base^{m-1} \cdot text[i] + text[i+1]
$$
由于數字太大時計算會溢出,用一個較大的素數來對結果取模$$ prime = 16777619 $$,最終得到哈希函數:
$$
hash(i+1, i+m]) = (hash(i, i+m-1) \cdot base - base^{m-1} \cdot text[i] + text[i+1]) % prime
$$
Rabin Fingerprint算法可以連續的處理字符串,在時間復雜度為$$ O(n) $$內求出所有字符串$$ text[i \dots i+m-1] $$的哈希值(其中$$ 0 \ge i \ge n-m+1 $$)。Rabin-Karp算法的時間復雜度為$$ O(n + z \cdot m) $$,其中$$ z $$是模式$$ pattern $$在文本中出現的次數。
--------
Rabin-Karp
* http://www.cs.cmu.edu/afs/cs/academic/class/15451-f14/www/lectures/lec6/karp-rabin-09-15-14.pdf
* https://www.geeksforgeeks.org/rabin-karp-algorithm-for-pattern-searching/
Rabin Fingerprint
* http://www.xmailserver.org/rabin.pdf
* https://pdos.csail.mit.edu/papers/lbfs:sosp01/lbfs.pdf
--------
#### 源碼
[RabinKarp.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/TextMatch/RabinKarp.h)
[RabinKarp.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/TextMatch/RabinKarp.cpp)
#### 測試
[RabinKarpTest.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/TextMatch/RabinKarpTest.cpp)
- Content 目錄
- Preface 前言
- Chapter-1 Sort 第1章 排序
- InsertSort 插入排序
- BubbleSort 冒泡排序
- QuickSort 快速排序
- MergeSort 歸并排序
- Chapter-2 Search 第2章 搜索
- BinarySearch 二分查找法(折半查找法)
- BruteForce 暴力枚舉
- Recursion 遞歸
- BreadthFirstSearch 廣度優先搜索
- BidirectionalBreadthSearch 雙向廣度搜索
- AStarSearch A*搜索
- DancingLink 舞蹈鏈
- Chapter-3 DataStructure 第3章 數據結構
- DisjointSet 并查集
- PrefixTree(TrieTree) 前綴樹
- LeftistTree(LeftistHeap) 左偏樹(左偏堆)
- SegmentTree 線段樹
- FenwickTree(BinaryIndexedTree) 樹狀數組
- BinarySearchTree 二叉查找樹
- AVLTree AVL平衡樹
- RedBlackTree 紅黑樹
- Chapter-4 DynamicProgramming 第4章 動態規劃
- Chapter-5 GraphTheory 第5章 圖論
- Chapter-6 Calculation 第6章 計算
- LargeNumber 大數字
- Exponentiation 求冪運算
- Chapter-7 CombinatorialMathematics 第7章 組合數學
- FullPermutation 全排列
- UniqueFullPermutation 唯一的全排列
- Combination 組合
- DuplicableCombination (元素)可重復的組合
- Subset 子集
- UniqueSubset 唯一的子集
- Permutation 排列
- PermutationGroup 置換群
- Catalan 卡特蘭數
- Chapter-8 NumberTheory 第8章 數論
- Sieve 篩選算法
- Euclid 歐幾里得
- EuclidExtension 歐幾里得擴展
- ModularLinearEquation 模線性方程
- ChineseRemainerTheorem 中國剩余定理
- ModularExponentiation 模冪運算
- Chapter-9 LinearAlgebra 第9章 線性代數
- Chapter-10 AnalyticGeometry 第10章 解析幾何
- Chapter-11 TextMatch 第11章 文本匹配
- SimpleMatch 簡單匹配
- AhoCorasickAutomata AC自動機
- KnuthMorrisPratt KMP匹配算法
- RabinKarp RabinKarp算法
- BoyerMoore BoyerMoore算法
- Chapter-12 GameTheory 第12章 博弈論
- BashGame 巴什博弈
- WythoffGame 威佐夫博弈
- NimGame 尼姆博弈