题解 | 字符统计

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

int main() {
    string str;
    cin >> str;

    // 1. 按 ASCII码的顺序从小到大排序
    sort(str.begin(), str.end());

    // 2. 有序map ,去重
    map<char, int> mp;
    for (char ch : str) {
        mp[ch] ++;
    }

    // 3. 按照从大到小的顺序,把次数存到set中,去重
    set<int, greater<>> cishu;
    for (auto & it : mp) {
        cishu.insert(it.second);
    }

    // 4. 按照次数进行遍历输出
    for (int c : cishu) {
        for (auto & it : mp) {
            if (it.second == c) {
                cout << it.first;
            }
        }
    }
    cout << endl;

    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

06-02 15:53
阳光学院 Java
点赞 评论 收藏
分享
吴offer选手:下午mt一来就告警说项目来不及,估计明天拿了权限就要参与开发了 已老实
实习生的蛐蛐区
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务