unordered_map,自定义sort,stringstream的应用
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<string,int>&a,pair<string,int>&b)
{
if(a.second!=b.second)
{
return a.second>b.second;
}
else {
return a.first<b.first;
}
}
int main() {
string s;
getline(cin,s);
stringstream ss(s);
string temp;
vector<pair<string,int>>res;
unordered_map<string,int>cnt;
while(ss>>temp)
{
cnt[temp]++;
}
for( auto it=cnt.begin();it!=cnt.end();it++)
{
if((*it).second>=3)
{
res.push_back({(*it).first,(*it).second});
}
}
sort(res.begin(),res.end(),cmp);
for(int i=0;i<res.size();i++)
cout<<res[i].first<<endl;
}
// 64 位输出请用 printf("%lld")

京公网安备 11010502036488号