题解 | #字符统计#
字符统计
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")