相关知识
unordered_map和unordered_set的适用范围和用法有何区别?
unordered_map适用于需要存储和查找键值对的场景,比如统计每种字符出现的次数。unordered_set适用于需要判断元素是否存在场景,比如统计出现了几种字符。哈希集合会自动去重,插入相同字符没有影响。
第一轮
最后一版(AC)
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_set>
using namespace std;
int main() {
string s;
getline(cin, s);
unordered_set<char> char_set;
for(char c : s){
char_set.insert(c);
}
cout << char_set.size() << endl;
}
// 64 位输出请用 printf("%lld")
第二轮
第一版(AC)
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
using namespace std;
int main() {
string s;
cin >> s;
int count = 0;
unordered_map<char, int> map;
for(char c : s){
map[c]++;
}
cout << map.size() << endl;
}
// 64 位输出请用 printf("%lld")
- 一开始没有理解题意,以为是要统计字符串中在范围内的总的字符数量。
- 后来觉得有问题,才发现问的是——字符串里有几种字符(0~127范围内)。
- 这一版用的是
unordered_map,第一轮的最后一版用的是unordered_set,注意两者的区别。