题解 | #字符统计#
字符统计
https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
bool compare(const pair<char, int> &a, const pair<char, int>& b)
{
if (a.second != b.second) {
return a.second > b.second; // 按照出现次数降序排列
}
else
{
return a.first < b.first; // 相同次数按照 char 的 ASCII 码升序排列
}
}
int main() {
string str;
map<char, int> map;
cin >> str;
for (auto x : str)
{
map[x]++;
}
vector<pair<char, int> > arr(map.begin(), map.end());
sort(arr.begin(), arr.end(), compare);
for (auto &p : arr)
{
cout << p.first;
}
return 0;
}
