题解 | 字符串哈希
字符串哈希
https://www.nowcoder.com/practice/dadbd37fee7c43f0ae407db11b16b4bf
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <set>
using namespace std;
//利用哈希表法,将表中没有的字符串,放入表中,然后输出长度
int main() {
//第一种方法我根据前面一道题目写了哈希表法得出的结果
// unordered_map<string , int> m;
// int N;
// string str;
// cin >> N;
// while (N--) {
// cin >> str;
// if(m.count(str) == 0){
// m[str] = N;//自动给字符串计算一个哈希值
// }
// }
// cout << m.size();
// return 0;
//第二种方法想到用集合也可以写出来
// set<string> s;
// int N;
// string str;
// cin >> N;
// while (N--) {
// cin >> str;
// if (s.count(str)==0){
// s.insert(str);
// }
// }
// cout << s.size();
// return 0;
//第三种看题解的,用的是哈希集合,似乎也是相较于集合多了个无序的提醒(set and unordered_set)
int n;
cin >> n;
unordered_set<string> distinct_strings;
string s;
for (int i = 0; i < n; i++) {
cin >> s;
distinct_strings.insert(s);
}
cout << distinct_strings.size() <<endl;
}
// 64 位输出请用 printf("%lld")
