字符串哈希模板

#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 @   duoluoluo  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示