题解 | #字符统计#

字符统计

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

map也可以传比较函数进去,不过注意map比较的是key,不是比较的pair.还有ASII码要升序排但次数要降序排

#include <algorithm>
#include <map>
#include <vector>


using namespace std;

bool compare( const char&lhs, const char&rhs){
    return lhs<rhs;
    
}

int main() {
    string str;
    map<char,int,decltype(compare)*> m(compare);
    while(cin>>str){
        for(int i = 0;i<str.size();i++){
           m[str[i]]++;
            
        }
        vector<char> v;
        for(auto it = m.begin();it!=m.end();it++){
            v.push_back((*it).first);
            
        }
        int n =v.size();
        char temp;
        for(int i=0;i<n;i++){
            
            for(int j = 0;j<n-i-1;j++){
                if(m[v[j]]<m[v[j+1]]){
                    swap(v[j],v[j+1]);
                }
            }
        }
        for(auto it:v){
            cout<<it;
        }
    }
}
全部评论

相关推荐

我要娶个什么名:学长你电脑闹鬼了
点赞 评论 收藏
分享
程序员花海_:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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