当前位置:网站首页>Buckle Exercise - 61 Sort by frequency of characters
Buckle Exercise - 61 Sort by frequency of characters
2022-08-10 11:34:00 【qq_43403657】
61 根据字符出现频率排序
1.问题描述
给定一个字符串,请将字符串里的字符按照出现的频率降序排列,如果频率相同,则按照字符的ASCII码升序排列.
示例 1:
输入:
“tree”
输出:
“eert”
解释:
'e’出现两次,'r’和’t’都只出现一次.
因此’e’必须出现在’r’和’t’之前,而且’r’比’t’的ASCII码小.
示例 2:
输入:
“cccaaa”
输出:
“aaaccc”
解释:
'c’和’a’都出现三次.So in ascending order of characters,'a’在’c’前.
示例 3:
输入:
“Aabb”
输出:
“bbAa”
解释:
'A’和’a’被认为是两种不同的字符,并且’A’的ASCII码比’a’小
2.输入说明
输入一个字符串
3.输出说明
输出一个字符串,Please refer to the above description for the order of characters in the string.
4.范例
输入
Aabb
输出
bbAa
5.代码
#include<iostream>
#include<map>
#include<string>
#include<unordered_map>
#include<algorithm>
using namespace std;
bool cmp(pair<char, int> a, pair<char, int>b)
{
return a.second == b.second?a.first<b.first:a.second>b.second;//该题目中最关键的部分,注意写法!
}
string OrderbyChar(string s)
{
//给定一个字符串,请将字符串里的字符按照出现的频率降序排列,如果频率相同,则按照字符的ASCII码升序排列.
string res;//结果字符串
unordered_map<char, int>hash;//记录每个字符出现次数
vector<pair<char, int> > v;//使用pair记录对应关系
for (auto c : s)//遍历字符串
hash[c]++;
for (auto it : hash)
v.emplace_back(it);//直接将每个hash元素插入v中
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < v.size(); i++)
{
while (v[i].second--)
res += v[i].first;
}
return res;
}
int main()
{
string s;
cin >> s;
string res = OrderbyChar(s);
cout << res << endl;
return 0;
}
边栏推荐
- POJ 2891 Strange Way to Express Integers (扩展欧几里得)
- Spss-多元回归案例实操
- Short video software development - how to break the platform homogenization
- STM32 encapsulation ESP8266 a key configuration function: implementations of AP mode and the STA mode switch, server and the client to create
- Will SQL and NoSQL eventually converge?
- Codeforces 814 C. An impassioned circulation of affection (dp)
- Three-phase 380V rectified voltage
- 【电商运营】你真的了解社交媒体营销(SMM)吗?
- 电脑怎么设置屏幕息屏时间(日常使用分享)
- Get started quickly and conquer three different distributed architecture calling schemes
猜你喜欢

runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function

第2章-矩阵及其运算-矩阵运算(2)

中小规模网站架构

基于UiAutomator2+PageObject模式开展APP自动化测试实战

推荐6个自媒体领域,轻松易上手

LAXCUS分布式操作系统安全管理

OneFlow source code parsing: operator instructions executed in a virtual machine

实现内网穿透的最佳解决方案(无实名认证,完全免费)
![[Go WebSocket] 多房间的聊天室(一)思考篇](/img/c9/4374a57c6a4ae02f606253a4c299e4.png)
[Go WebSocket] 多房间的聊天室(一)思考篇

Get started quickly and conquer three different distributed architecture calling schemes
随机推荐
即时零售业态下如何实现自动做账?
ENVI 5.3软件安装包和安装教程
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
怎么加入自媒体,了解这5种变现模式,让账号快速变现
关于振弦采集模块及采集仪振弦频率值准确率的问题
基于UiAutomator2+PageObject模式开展APP自动化测试实战
L2 applications from a product perspective: why is it a playground?
Open Office XML 格式里如何描述多段具有不同字体设置的段落
一文带你搞懂中断按键驱动程序之poll机制
Nocalhost - 让云原生时代的开发更高效
Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan
Gartner reiterates the important value of 'data weaving'
Licking Exercise - 58 Verifying Binary Search Trees
第5章相似矩阵及二次型(4)
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
基于UiAutomator2+PageObject模式开展APP自动化测试实战
力扣练习——60 二叉搜索子树的最大键值和
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
振弦传感器及核心VM系列振弦采集模块
为什么Redis很快