题目大意
给出n个字符串。要你求有多少个不同的字符串。
解
一个哈希不保险,双重哈希【就是一个串求两个模数不同的哈希值】
代码
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int k1, k2;
int n, ls, t, flag, ans, l[20000];
string s;
struct asdf {
int next, hashh;
} a[20000];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
cin >> s;
ls = s.size();
k1 = k2 = 0;
flag = 0;
for (int j = 0; j < ls; ++j) k1 = (k1 * 130 + s[j]) % 13331; //哈希1
for (int j = 0; j < ls; ++j) k2 = (k2 * 130 + s[j]) % 10003; //哈希2
if (l[k1] != 0) {
//如果第一个哈希值对上了
for (int j = l[k1]; j; j = a[j].next) {
//邻接表储存
if (a[j].hashh == k2) {
//两个哈希值都一样,说明该串曾找到过
flag = 1;
break;
}
}
if (flag == 0) {
//不一样,邻接表接上
++ans;
++t;
a[t].next = l[k1];
a[t].hashh = k2;
l[k1] = t;
}
} else {
//新建
++ans;
++t;
a[t].next = l[k1];
a[t].hashh = k2;
l[k1] = t;
}
}
printf("%d", ans);
}