字符串哈希
Description–
高效进阶「字符串算法」第2章 Hash 和 Hash 表课堂过关 例题1
代码–
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int M = 1e8 + 9;
int s, n, ans;
bool pd[M];
string o;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
s = 0;
cin >> o;
for (int j = 0; j < (int)o.size(); ++j)
s = ((long long)s * 131 + o[j]) % M;
if (!pd[s]) ans++, pd[s] = 1;
}
printf("%d", ans);
return 0;
}