当前位置:网站首页>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;
}
边栏推荐
- 第2章-矩阵及其运算-矩阵创建(1)
- [Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
- StoneDB 文档捉虫活动第一季
- Unsafe的一些使用技巧
- 杭电多校-Loop-(不确定性贪心+线段树)
- Store limited time seckill function system
- blocking non-blocking poll mechanism asynchronous
- Codeforces 814 C. An impassioned circulation of affection (dp)
- Article take you understand interrupt the key driver of polling mechanism
- POJ 1026 Cipher (Permutation Groups)
猜你喜欢
![[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists](/img/06/9d49fc99ab684f03740deb2abc38e2.png)
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists

建校仅11年就入选“双一流” ,这所高校是凭什么做到的?

3款不同类型的自媒体免费工具,有效提高创作、运营效率

Emulate stm32 directly with proteus - the programmer can be completely discarded

自媒体爆款标题怎么写?手把手教你写热门标题

从脚本到剪辑,影像大师亲授的后期制作秘籍

Several small projects that I have open sourced over the years

接口定义与实现

【勇敢饭饭,不怕刷题之链表】链表中有环的问题

石墨文档打开文档时快速定位到上次写的位置
随机推荐
C#实战:基于ItextSharp技术标签生成小工具
LeetCode_152_乘积最大子数组
STM32 encapsulation ESP8266 a key configuration function: implementations of AP mode and the STA mode switch, server and the client to create
ENVI 5.3软件安装包和安装教程
2023版揽胜运动曝光,安全、舒适一个不落
【无标题】
力扣练习——60 二叉搜索子树的最大键值和
Gartner reiterates the important value of 'data weaving'
【机器学习】浅谈正规方程法&梯度下降
Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
使用哈工大LTP测试分词并且增加自定义字典
OSSCore 开源解决方案介绍
Mobile and PC compatible loading and toast message plugins
Redis设计与实现
Clicking Exercise - 64 Longest Harmonic Subsequences
从源码角度分析UUID的实现原理
Where can I view the version record of WeChat applet submission review history?
Nocalhost - 让云原生时代的开发更高效
不止跑路,拯救误操作rm -rf /*的小伙儿
力扣练习——56 寻找右区间