题解 | 字符统计

字符统计

https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0

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

int main() {
    string s;
    cin >> s;
    map<char, int> m;
    for(const char& c: s) {
        ++m[c];
    }
    vector<pair<char, int>> vec(m.begin(), m.end());
    sort(vec.begin(), vec.end(), [](auto& a, auto& b) {
        if(a.second > b.second) return true;
        else if(a.second == b.second) return a.first < b.first; //数量相等时按字典序从小到大排序
        else return false;
    });
    for(auto& it: vec) {
        cout << it.first;
    }
    return 0;
}

全部评论

相关推荐

积极的小学生不要香菜:你才沟通多少,没500不要说难
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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