diff --git a/Strings/slides/hashes/codes/double_hash.cpp b/Strings/slides/hashes/codes/double_hash.cpp index 676d4ee8..e7d890c0 100644 --- a/Strings/slides/hashes/codes/double_hash.cpp +++ b/Strings/slides/hashes/codes/double_hash.cpp @@ -6,12 +6,13 @@ int f(char c) { return c - 'a' + 1; } int hi(long long pi, long long qi, const string& s) { + int N = s.size(); long long ans = 0; - for (auto c : s) + for (int i = N - 1; i >= 0; --i) { ans = (ans * pi) % qi; - ans = (ans + f(c)) % qi; + ans = (ans + f(s[i])) % qi; } return ans; diff --git a/Strings/slides/hashes/codes/h.cpp b/Strings/slides/hashes/codes/h.cpp index 7f3b1e5c..04f25e11 100644 --- a/Strings/slides/hashes/codes/h.cpp +++ b/Strings/slides/hashes/codes/h.cpp @@ -5,12 +5,13 @@ int f(char c) int h(const string& s) { + int N = s.size(); long long ans = 0, p = 31, q = 1'000'000'007; - for (auto c : s) + for (int i = N - 1; i >= 0; --i) { ans = (ans * p) % q; - ans = (ans + f(c)) % q; + ans = (ans + f(s[i])) % q; } return ans; diff --git a/Strings/slides/hashes/codes/subs.cpp b/Strings/slides/hashes/codes/subs.cpp index 0f5fed87..f97f70a7 100644 --- a/Strings/slides/hashes/codes/subs.cpp +++ b/Strings/slides/hashes/codes/subs.cpp @@ -11,12 +11,13 @@ int f(char c) int h(const string& s) { + int N = s.size(); ll ans = 0; - for (auto c : s) + for (int i = N - 1; i >= 0; --i) { ans = (ans * p) % q; - ans = (ans + f(c)) % q; + ans = (ans + f(s[i])) % q; } return ans; @@ -72,8 +73,8 @@ int h(int i, int j, const vector& ps, const vector& is) int unique_substrings(const string& s) { - set hs; int N = s.size(); + set hs; auto ps = prefixes(s); auto is = inverses(s.size());