使用哈希表存储字符串出现的次数,
每当有新的字符串输入时,哈希表中加入一个新的键-值对,当输入为已经出现过的字符串时,这个键对于的值+1;
最后只需要输出哈希表中键的个数即可.
#include <iostream>
#include<unordered_map>
using namespace std;
int main() {
int n;
cin>>n;
unordered_map<string,int> map;
while(n--){
string s;
cin>>s;
if(map.count(s)){
map[s]+=1;
}else{
map.emplace(s,1);
}
}
int num = map.size();
cout<<num;
}

京公网安备 11010502036488号