题目链接
实现方法
- 统计各字符出现的次数;
- 判断是否能实现重排(根据出现次数最多的字符数量ma和字符总长度n判断);
- 依次输出出现次数最多的两个字符,直到出现次数最多的字符和次多的字符数量相同;
- 依次输出剩余的所有字符;
代码
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
//ma记录出现次数最多字符的数量
int n,ma=0;
string ss;
cin>>n>>ss;
map<char,int>m;
for(auto i:ss){
m[i]++;