字符串哈希模板

#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1000010, hashBase[2] = {29, 31}, hashMod[2] = {1000000007, 1000000009}, hashNum = 2;
int n, pre[N][2], power[N][2];
char s[N];
bool check (int l1, int r1, int l2, int r2) {
	for (int i = 0; i < hashNum; i ++) {
		int x = (pre[r1][i] - 1ll * pre[l1 - 1][i] * power[r1 - l1 + 1][i] % hashMod[i] + hashMod[i]) % hashMod[i];
		int y = (pre[r2][i] - 1ll * pre[l2 - 1][i] * power[r2 - l2 + 1][i] % hashMod[i] + hashMod[i]) % hashMod[i];
		if (x != y) return 0;
	}
	return 1;
}
int main () {
	scanf("%d", &n);
	scanf("%s", s + 1);
	power[0][0] = power[0][1] = 1;
	for (int i = 1; i <= n; i ++)
		for (int j = 0; j < hashNum; j ++) {
			power[i][j] = 1ll * power[i - 1][j] * hashBase[j] % hashMod[j];
			pre[i][j] = (1ll * pre[i - 1][j] * hashBase[j] + s[i]) % hashMod[j];
		}
	return 0;
}
posted @ 2022-02-10 15:56  duoluoluo  阅读(63)  评论(0)    收藏  举报