これは、O((m+n)*log(m+n)) で終了できる単純なアルゴリズムであり、O(m+n) ランタイムであるサフィックス ツリー アルゴリズムと比較して実装がはるかに簡単です。
最小共通長 (minL) = 0、最大共通長 (maxL) = min(m+n)+1 で開始します。
1. if (minL == maxL - 1), the algorithm finished with common len = minL.
2. let L = (minL + maxL)/2
3. hash every substring of length L in S, with key = hash, val = startIndex.
4. hash every substring of length L in T, with key = hash, val = startIndex. check if any hash collision in to hashes. if yes. check whether whether they are really common substring.
5. if there're really common substring of length L, set minL = L, otherwise set maxL = L. goto 1.
残りの問題は、時間 O(n) で長さ L のすべての部分文字列をハッシュする方法です。次のように多項式を使用できます。
Hash(string s, offset i, length L) = s[i] * p^(L-1) + s[i+1] * p^(L-2) + ... + s[i+L-2] * p + s[i+L-1]; choose any constant prime number p.
then Hash(s, i+1, L) = Hash(s, i, L) * p - s[i] * p^L + s[i+L];