题解 | #字符统计#

字符统计

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")
全部评论

相关推荐

04-13 18:10
门头沟学院 Java
想熬夜的小飞象在秋招:被腾讯挂了后爸妈以为我失联了
点赞 评论 收藏
分享
双非阴暗爬行:我来看看笑死我了,这名字取得好想笑(没有不好的意思)
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务