From 0018dbf69f7212f5440788599557479535405598 Mon Sep 17 00:00:00 2001 From: subcrip Date: Wed, 10 Apr 2024 13:53:42 +0800 Subject: [PATCH] Add string/slice_hash.cc --- string/slice_hash.cc | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 string/slice_hash.cc diff --git a/string/slice_hash.cc b/string/slice_hash.cc new file mode 100644 index 0000000..f38c296 --- /dev/null +++ b/string/slice_hash.cc @@ -0,0 +1,23 @@ +struct slice_hash { + using hash_type = pair, MLL>; + int n; + vector> pw1; + vector> pw2; + vector> hash1; + vector> hash2; + slice_hash(const string& s) : n(s.size()), pw1(n + 1), pw2(n + 1), hash1(n + 1), hash2(n + 1) { + constexpr int b = 31; + pw1[0] = 1, pw2[0] = 1; + for (int i = 1; i <= n; ++i) { + hash1[i] = hash1[i - 1] + s[i - 1] * pw1[i - 1]; + hash2[i] = hash2[i - 1] + s[i - 1] * pw2[i - 1]; + pw1[i] = pw1[i - 1] * b; + pw2[i] = pw2[i - 1] * b; + } + } + + // query [l, r] + hash_type hash(int l, int r) { + return { (hash1[r + 1] - hash1[l]) / pw1[l], (hash2[r + 1] - hash2[l]) / pw2[l] }; + } +};