字符串自然溢出哈希/单哈希/双哈希模板

自然溢出哈希

struct Hash {
    using u64 = unsigned long long;
    u64 base = 13331;
    vector<u64> pow, hash;
    Hash(string &s) {
        s = " " + s;
        int N = s.size();
        pow.resize(N + 1), hash.resize(N + 1);
        pow[0] = 1, hash[0] = 0;
        for (int i = 1; i < s.size(); i ++) {
            pow[i] = pow[i - 1] * base;
            hash[i] = hash[i - 1] * base + s[i];
        }
    }

    u64 get(int l, int r) {
        return hash[r] - hash[l - 1] * pow[r - l + 1];
    }

    //拼接两个子串
    u64 link(int l1, int r1, int l2, int r2) {
        return get(l1, r1) * pow[r2 - l2 + 1] + get(l2, r2);
    }

    bool same(int l1, int r1, int l2, int r2) {
        return get(l1, r1) == get(l2, r2);
    }

};

单哈希加强版

已过洛谷卡单哈希模数数据 字符串 Hash(数据加强)提交记录

struct Hash {
	using i64 = long long;
	using u64 = unsigned i64;
	u64 base = 1000000033, mod = 212370440130137957ll;
	vector<u64> pow, hash;
	Hash() {}
	Hash(string &s) {
		init(s);
	}
	void init(string &s) {
		s = " " + s;
		int N = s.size();
		pow.resize(N + 1), hash.resize(N + 1);
		pow[0] = 1, hash[0] = 0;
		for (int i = 1; i < N; i ++) {
			pow[i] = pow[i - 1] * base % mod;
			hash[i] = (hash[i - 1] * base % mod + s[i]) % mod;
		}
	}
	u64 get(int l, int r) {
		return (hash[r] - hash[l - 1] * pow[r - l + 1] % mod + mod) % mod ;
	}
	//拼接两个子串
	u64 link(int l1, int r1, int l2, int r2) {
		return (get(l1, r1) * pow[r2 - l2 + 1] % mod + get(l2, r2)) % mod;
	}
	bool same(int l1, int r1, int l2, int r2) {
		return get(l1, r1) == get(l2, r2);
	}
};

双哈希

struct DoubleHash {
	using i64 = long long;
	const i64 Base1 = 29, MOD1 = 1e9 + 7;
	const i64 Base2 = 131, MOD2 = 1e9 + 9;
	vector<i64> ha1, ha2, pow1, pow2;
	vector<i64> rha1, rha2;
	int len;
	DoubleHash() {}
	DoubleHash(string &s) {
		init(s);
	}
	void init(string &s) {
		len = s.size();
		ha1.resize(len + 1), ha2.resize(len + 1);
		pow1.resize(len + 1), pow2.resize(len + 1);
		rha1.resize(len + 1), rha2.resize(len + 1);
		s = " " + s;
		pow1[0] = pow2[0] = 1;
		for (int i = 1; i <= len; i++) {
			pow1[i] = pow1[i - 1] * Base1 % MOD1;
			pow2[i] = pow2[i - 1] * Base2 % MOD2;
		}
		for (int i = 1; i <= len; i++) {
			ha1[i] = (ha1[i - 1] * Base1 + s[i]) % MOD1;
			ha2[i] = (ha2[i - 1] * Base2 + s[i]) % MOD2;
			rha1[i] = (rha1[i - 1] * Base1 + s[len - i + 1]) % MOD1;
			rha2[i] = (rha2[i - 1] * Base2 + s[len - i + 1]) % MOD2;
		}
	}
	pair<i64, i64> get(int l, int r) {
		i64 res1 = ((ha1[r] - ha1[l - 1] * pow1[r - l + 1]) % MOD1 + MOD1) % MOD1;
		i64 res2 = ((ha2[r] - ha2[l - 1] * pow2[r - l + 1]) % MOD2 + MOD2) % MOD2;
		return {res1, res2};
	}
	//反哈希
	pair<i64, i64> get_rhash(int l, int r) {
		i64 res1 = ((rha1[len - l + 1] - rha1[len - r] * pow1[r - l + 1]) % MOD1 + MOD1) % MOD1;
		i64 res2 = ((rha2[len - l + 1] - rha2[len - r] * pow2[r - l + 1]) % MOD2 + MOD2) % MOD2;
		return {res1, res2};
	}
	//判断s[l, r]是否为回文串
	bool is_palindrome(int l, int r) {
		return get(l, r) == get_rhash(l, r);
	}
	pair<i64, i64> add(pair<i64, i64> aa, pair<i64, i64> bb) {
		i64 res1 = (aa.first + bb.first) % MOD1;
		i64 res2 = (aa.second + bb.second) % MOD2;
		return {res1, res2};
	}
	//aa *= Base的k次方
	pair<i64, i64> mul(pair<i64, i64> aa, i64 kk) {
		i64 res1 = aa.first * pow1[kk] % MOD1;
		i64 res2 = aa.second * pow2[kk] % MOD2;
		return {res1, res2};
	}
	//拼接字符串 r1 < l2  s = s1 + s2
	pair<i64, i64> link(int l1, int r1, int l2, int r2) {
		return add(mul(get(l2, r2), r1 - l1 + 1), get(l1, r1));
	}
};
posted @ 2024-07-25 20:01  Ke_scholar  阅读(120)  评论(1编辑  收藏  举报