【模板】字符串哈希
题目描述
如题,给定
友情提醒:如果真的想好好练习哈希的话,请自觉。
输入格式
第一行包含一个整数
接下来
输出格式
输出包含一行,包含一个整数,为不同的字符串个数。
样例 #1
样例输入 #1
5
abc
aaaa
abc
abcc
12345样例输出 #1
4提示
对于
对于
对于
样例说明:
样例中第一个字符串(abc)和第三个字符串(abc)是一样的,所以所提供字符串的集合为{aaaa,abc,abcc,12345},故共计4个不同的字符串。
Tip: 感兴趣的话,你们可以先看一看以下三题:
BZOJ3097:http://www.lydsy.com/JudgeOnline/problem.php?id=3097
BZOJ3098:http://www.lydsy.com/JudgeOnline/problem.php?id=3098
BZOJ3099:http://www.lydsy.com/JudgeOnline/problem.php?id=3099
如果你仔细研究过了(或者至少仔细看过AC人数的话),我想你一定会明白字符串哈希的正确姿势的
题解
利用unsigned long long自然溢出
c++
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
void solve() {
int n;
cin >> n;
set<u64> s;
constexpr int base = 131;
auto Hash = [&](string S) {
u64 ans = 0;
for (auto i : S) {
ans = ans * base + i;
}
return ans;
};
for (int i = 0; i < n; i++) {
string S;
cin >> S;
s.insert(Hash(S));
}
cout << s.size() << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
return 0;
}