输入dgccc 输出cccdg
输入dgggCcc 输出gggccdC
输入一个字符串,按照字符串中字符的数量从大到小输出
#include<iostream>
#include<unordered_map>
#include<string>
#include<algorithm>
using namespace std;
bool comp(const pair<char, int>& a, const pair<char, int> &b) {
return a.second > b.second;
}
int main() {
string s;
cin >> s;
unordered_map<char, int> mp;
for (char c : s) {
mp[c]++;
}
vector<pair<char, int> > b;
for (auto x : mp) {
b.push_back(x);
}
sort(b.begin(), b.end(), comp);
string res;
for (int i=0; i<b.size(); ++i) {
for (int j = 0; j < b[i].second; ++j) {
res += b[i].first;
}
}
cout << res<< endl;
}