存一个hash板子

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Cymbals/article/details/83795236
struct Hash {
	ull hs[maxn], hs2[maxn];
	ull	p[maxn], p2[maxn];
	int seed = 31, seed2 = 233;

	void init(char *s) {
		p[0] = p2[0] = 1;
		hs[0] = hs2[0] = s[0] - 'a';
		for(int i = 1, c; s[i]; i++) {
			c = s[i] - 'a';
			hs[i] = hs[i - 1] * seed + c;
			hs2[i] = hs2[i - 1] * seed2 + c;
			p[i] = p[i - 1] * seed;
			p2[i] = p2[i - 1] * seed2;
		}
	}

	pair<ull, ull> hash(int l, int r) {
		ull tmp = hs[l], tmp2 = hs2[r];
		tmp -= hs[l - 1] * p[r - l + 1];
		tmp2 -= hs2[l - 1] * p2[r - l + 1];
		return make_pair(tmp, tmp2);
	}

} S, T;

猜你喜欢

转载自blog.csdn.net/Cymbals/article/details/83795236