From 313c9b12d95c3213c28d01468efefa7a225d166c Mon Sep 17 00:00:00 2001 From: yogi Date: Sun, 15 Dec 2024 23:45:20 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Corre=C3=A7=C3=A3o:=20ordem=20dos=20loops?= =?UTF-8?q?=20de=20h()=20deve=20ser=20reversa=20(assim=20como=20estava=20n?= =?UTF-8?q?o=20commit=20anterior)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Strings/slides/hashes/codes/double_hash.cpp | 3 ++- Strings/slides/hashes/codes/h.cpp | 3 ++- Strings/slides/hashes/codes/subs.cpp | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Strings/slides/hashes/codes/double_hash.cpp b/Strings/slides/hashes/codes/double_hash.cpp index 676d4ee8..5ea2fbf3 100644 --- a/Strings/slides/hashes/codes/double_hash.cpp +++ b/Strings/slides/hashes/codes/double_hash.cpp @@ -6,9 +6,10 @@ 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; diff --git a/Strings/slides/hashes/codes/h.cpp b/Strings/slides/hashes/codes/h.cpp index 7f3b1e5c..4fec5960 100644 --- a/Strings/slides/hashes/codes/h.cpp +++ b/Strings/slides/hashes/codes/h.cpp @@ -5,9 +5,10 @@ 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; diff --git a/Strings/slides/hashes/codes/subs.cpp b/Strings/slides/hashes/codes/subs.cpp index 0f5fed87..7a78528a 100644 --- a/Strings/slides/hashes/codes/subs.cpp +++ b/Strings/slides/hashes/codes/subs.cpp @@ -11,9 +11,10 @@ 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; @@ -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()); From 03113e8daf098180ead142b3233c47dba860f86c Mon Sep 17 00:00:00 2001 From: yogi Date: Mon, 16 Dec 2024 15:13:05 -0300 Subject: [PATCH 2/2] =?UTF-8?q?Corre=C3=A7=C3=A3o:=20com=20a=20mudan=C3=A7?= =?UTF-8?q?a=20do=20for=20precisava=20arrumar=20a=20vari=C3=A1vel=20tamb?= =?UTF-8?q?=C3=A9m?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Strings/slides/hashes/codes/double_hash.cpp | 2 +- Strings/slides/hashes/codes/h.cpp | 2 +- Strings/slides/hashes/codes/subs.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Strings/slides/hashes/codes/double_hash.cpp b/Strings/slides/hashes/codes/double_hash.cpp index 5ea2fbf3..e7d890c0 100644 --- a/Strings/slides/hashes/codes/double_hash.cpp +++ b/Strings/slides/hashes/codes/double_hash.cpp @@ -12,7 +12,7 @@ int hi(long long pi, long long qi, const string& 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 4fec5960..04f25e11 100644 --- a/Strings/slides/hashes/codes/h.cpp +++ b/Strings/slides/hashes/codes/h.cpp @@ -11,7 +11,7 @@ int h(const string& 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 7a78528a..f97f70a7 100644 --- a/Strings/slides/hashes/codes/subs.cpp +++ b/Strings/slides/hashes/codes/subs.cpp @@ -17,7 +17,7 @@ int h(const string& 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;