双哈希模板

我以前比较懒, \(hash\) 该学的时候没学, 现在来补一下。


如题, 就是双哈希过洛谷模板题(字符串哈希)。
因为我觉得双哈希似乎比较稳(但是它确实慢qwq)。

Luogu数据AC代码

#include<bits/stdc++.h>
using namespace std;
const int mod1 = 10000007;
const int mod2 = 13333337;
int base1, base2;
int h1[10005], h2[10005];
bool eq(int i,int j) {
	return (h1[i]==h1[j] && h2[i]==h2[j]);
}

int n;
char s[10005][1505];
int main()
{
	srand((unsigned)time(0));
	base1 = rand()%100+200, base2 = rand()%300+400;
	
	cin >> n; for(int i=1;i<=n;++i) scanf("%s", s[i]);
	for(int i=1;i<=n;++i) {
		int len = strlen(s[i]);
		for(int j=0;j<len;++j)
		{
			h1[i] = (h1[i]*base1+s[i][j]) % mod1;
			h2[i] = (h2[i]*base2+s[i][j]) % mod2;
		}
	}
	int ans = n;
	for(int i=1; i<=n; ++i)
		for(int j=i-1;j>=1;--j)
		if(eq(i,j)){
			--ans;
			break;
		}
	cout << ans;
	return 0;
}
posted @ 2020-04-27 11:24  xwmwr  阅读(520)  评论(0编辑  收藏  举报