题解 | #字符统计#

字符统计

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

我以为我把简单问题复杂话了,看了一圈答案发现并没有,自己写的还是挺简洁的。

#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
struct key{
    int _c;
    int _times;
    key(char c, char times) : _c(c), _times(times){}
    bool operator<(const struct key& k) const{
        return _times > k._times || (k._times == _times && _c < k._c);
    }
};
using key = struct key;

int main() {
    string input;
    cin >> input;
    vector<int> vst(128, 0);
    set<key> sk;
    for (const auto & x: input) {
        vst[x]++;
    }
    for (int i = 0; i < vst.size(); ++i){
        if(vst[i] != 0)
        sk.insert(key(i, vst[i])); 
    }
    for(const auto &x : sk){
        cout << (char)x._c;
    }
}
// 64 位输出请用 printf("%lld")
全部评论

相关推荐

mjasjon:这种trash中厂 简历过筛概率比大厂还低(除阿里系)
投递哔哩哔哩等公司7个岗位
点赞 评论 收藏
分享
真烦好烦真烦:豆包润色了自己没看看吗,再说了,都说豆包是愚蠢且勤快的大学生,ds才是聪明的研究生,怎么敢让豆包写论文的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务