华为oj之字符统计(按出现次数由多到少的顺序进行输出)

题目: 字符统计

  • 热度指数:875 时间限制:1秒 空间限制:32768K

  • 本题知识点: 字符串 排序

题目描述

如果统计的个数相同,则按照ASII码由小到大排序输出 。如果有其他字符,则对这些字符不用进行统计。

实现以下接口:
输入一个字符串,对字符中的各个英文字符,数字,空格进行统计(可反复调用)
按照统计个数由多到少输出统计结果,如果统计的个数相同,则按照ASII码由小到大排序输出
清空目前的统计结果,重新统计

调用者会保证:
输入的字符串以‘\0’结尾。

输入描述:

输入一串字符。

输出描述:

对字符中的各个英文字符(大小写分开统计),数字,空格进行统计,并按照统计个数由多到少输出,如果统计的个数相同,则按照ASII码由小到大排序输出。如果有其他字符,则对这些字符不用进行统计。

输入例子:
aadddccddc
输出例子:
dca

在线提交网址:
http://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0?tpId=37&tqId=21325&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking

分析:

使用一个hash表(unordered_map)存储 <字符, 出现次数> 的键值对, 然后按出现次数count进行排序(C++算法库中的sort函数), 排序时注意当统计的个数相同时, 按照ASII码由小到大顺序输出.

下面是已AC代码:

#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool comp(pair<char,int> a, pair<char,int> b)  // 重载sort函数的自定义比较函数comp
{
    if(a.second > b.second)
        return true;
    else if(a.second == b.second && a.first < b.first)
        return true;
    else return false;
}

int main()
{
    string str;
    unordered_map<char, int> countMap;

    while(getline(cin,str))
    {
    for(int i=0; i <= str.length()-1; i++)
        {
             if(countMap.find(str[i]) == countMap.end())
            {
                countMap[str[i]] = 1;
            }
            else
            {
                countMap[str[i]]++;
            }        
        }

    vector<pair<char, int>> elems(countMap.begin(), countMap.end());
    sort(elems.begin(), elems.end(), comp);  // 使用sort对hash表进行排序

    for(vector<pair<char, int>>::iterator it=elems.begin(); it != elems.end(); it++)
    {
        cout<<it->first;
    }
    cout<<endl;
    countMap.clear();
    }
    return 0;
}
测试用例1: 8v26ktzk069lm400061m0v965we88850o6omqi532ktir6esb55t0kqm026y8rk63aj82kcx48gd1tiylvs0xo32zem65q7z5ce2185d2ascz62a2p3ajr45h637t2p290lc043gicp5ldzzmx2

测试用例2:
aadddccddc

参考:
http://stackoverflow.com/questions/31323135/sort-an-unordered-map-using-sort

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大白技术控

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值