From ee1189f090ab3c8b6f9354608c4b49d0eea00667 Mon Sep 17 00:00:00 2001 From: Code Lich Date: Wed, 23 Apr 2025 14:49:24 +0700 Subject: [PATCH] add rolling hash snippet --- lua/custom/snippets/edhliicpp.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lua/custom/snippets/edhliicpp.lua b/lua/custom/snippets/edhliicpp.lua index a701a737..f6e5b007 100644 --- a/lua/custom/snippets/edhliicpp.lua +++ b/lua/custom/snippets/edhliicpp.lua @@ -216,4 +216,29 @@ ls.add_snippets('cpp', { '};', } ), + s('rollinghash', { + t { + 'struct RollingHash {', + ' using ll = long long;', + ' ll base, mod;', + ' vector power;', + ' vector prefix_hash;', + ' RollingHash(const string &s, ll base, ll mod) : base(base), mod(mod) {', + ' int n = s.size();', + ' power.resize(n + 1, 1);', + ' prefix_hash.resize(n + 1, 0);', + ' for (int i = 0; i < n; ++i) {', + ' power[i + 1] = (__int128)power[i] * base % mod;', + ' prefix_hash[i + 1] = ((__int128)prefix_hash[i] * base + s[i]) % mod;', + ' }', + ' }', + ' ll get_hash(int l, int r) {', + ' ll hash_r = prefix_hash[r + 1];', + ' ll hash_l = (__int128)prefix_hash[l] * power[r - l + 1] % mod;', + ' ll res = (hash_r - hash_l + mod) % mod;', + ' return res;', + ' }', + '};', + }, + }), })