字符统计

字符统计

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

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

struct Node
{
    char c;
    unsigned cnt;
};

bool cmp(Node a, Node b)
{
    if(a.cnt != b.cnt) return a.cnt > b.cnt;
    else return a.c < b.c;
}

int main()
{
    string str;
    unsigned hashTable[200];
    while(cin >> str)
    {
        memset(hashTable, 0, sizeof(hashTable));
        for(auto it = str.begin(); it != str.end(); ++it)
        {
            hashTable[*it]++;
        }
        vector<Node> vec;
        Node node;
        for(char i = '0'; i <= 'z'; ++i)
        {
            if(!hashTable[i]) continue;
            node.c = i;
            node.cnt = hashTable[i];
            vec.push_back(node);
        }
        sort(vec.begin(), vec.end(), cmp);
        for(Node a : vec)
        {
            cout << a.c;
        }
        cout << endl;
    }

    return 0;
}
全部评论

相关推荐

半解316:内容充实,细节需要修改一下。 1,整体压缩为一页。所有内容顶格。 2,项目描述删除,直接写个人工作量 修改完之后还需要建议,可以私聊
点赞 评论 收藏
分享
ZywOo_求职版:谁问你了....
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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