题解 | #字符统计#
字符统计
http://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
#include<bits/stdc++.h>
using namespace std;
//排序逻辑
bool compare(pair<char, int> a, pair<char, int> b){
if(a.second != b.second){
return a.second > b.second;
}else{
return a.first < b.first;
}
}
int main(){
string s;
while(cin >> s){
//map来统计字符出现次数
map<char, int> mp;
for(int i = 0; i < s.size(); i++){
if(mp.find(s[i]) != mp.end()){
mp[s[i]]++;
}else{
mp[s[i]] = 1;
}
}
//创建数组,便于排序
vector<pair<char, int>> arr;
for(auto it : mp){
arr.push_back(make_pair(it.first, it.second));
}
//排序并输出
sort(arr.begin(), arr.end(), compare);
for(int i = 0; i < arr.size(); i++){
cout << arr[i].first;
}
cout << endl;
}
return 0;
}