Skip to content

Commit

Permalink
Minimize Hash (w/o test)
Browse files Browse the repository at this point in the history
  • Loading branch information
oToToT authored Oct 12, 2023
1 parent f39e262 commit d474a14
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions codes/String/Hash.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
class Hash {
private:
static constexpr int P = 127, Q = 1051762951;
vector<int> h, p;
public:
void init(const string &s){
h.assign(s.size()+1, 0); p.resize(s.size()+1);
for (size_t i = 0; i < s.size(); ++i)
h[i + 1] = add(mul(h[i], P), s[i]);
generate(p.begin(), p.end(),[x=1,y=1,this]()
mutable{y=x;x=mul(x,P);return y;});
}
int query(int l, int r){ // 1-base (l, r]
return sub(h[r], mul(h[l], p[r-l]));}
private:
static constexpr int P = 127, Q = 1051762951;
vector<int> h, p;
public:
Hash(string_view s):h(s.size()+1),p(s.size()+1){
for (size_t i = 0; i < s.size(); ++i)
h[i + 1] = add(mul(h[i], P), s[i]);
generate(p.begin(), p.end(),[x=1,y=1,this]()
mutable{y=x;x=mul(x,P);return y;});
}
int query(int l, int r){ // 1-base (l, r]
return sub(h[r], mul(h[l], p[r-l]));}
};

0 comments on commit d474a14

Please sign in to comment.