設字串陣列為S[n],字元編號為0到n-1
首先建立陣列Hash[n+1],Hash[i]表示
(S[0]*P^{i-1}+S[1]*P^{i-2}+...+S[i-1])\%PM,Hash[0]=0,其中P跟PM是質數
假設要取編號L到編號R左閉右開區間的hash值,則其為(Hash[R]-Hash[L]*P^{R-L})\%PM
如果要用double hash則用不同質數計算兩種不同的hash值,用pair拼起來即可
以下提供模板:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <unsigned P, unsigned PM> class RollingHash { | |
static vector<unsigned> Base; | |
vector<unsigned> Hash; | |
using LL = long long; | |
public: | |
RollingHash(const string &S) : Hash(S.size() + 1) { | |
if (Base.empty()) | |
Base.resize(1, 1); | |
for (size_t i = 1; i <= S.size(); ++i) | |
Hash[i] = (LL(Hash[i - 1]) * P + S[i - 1]) % PM; | |
while (Base.size() < Hash.size()) | |
Base.emplace_back((LL(Base.back()) * P) % PM); | |
} | |
size_t size() const { return Hash.size() - 1; } | |
const vector<unsigned> &getHash() const { return Hash; } | |
unsigned hash() const { return Hash.back(); } | |
unsigned hash(unsigned L, unsigned R) const { | |
return (Hash[R] + PM - (LL(Hash[L]) * Base[R - L]) % PM) % PM; | |
} | |
}; | |
template <unsigned P, unsigned PM> vector<unsigned> RollingHash<P, PM>::Base; |
沒有留言:
張貼留言