-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_sort.cpp
More file actions
49 lines (42 loc) · 718 Bytes
/
custom_sort.cpp
File metadata and controls
49 lines (42 loc) · 718 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
using namespace std;
class Freq
{
public:
char value;
int count;
};
bool cmp(Freq a, Freq b)
{
if (a.count == b.count)
return a.value < b.value;
else
return a.count > b.count;
}
int main()
{
string s;
cin >> s;
Freq f[26];
for (int i = 0; i < 26; i++)
{
f[i].value = char(i + 'a');
f[i].count = 0;
}
for (char c : s)
{
f[int(c - 'a')].count++;
}
sort(f, f + 26, cmp);
for (int i = 0; i < 26; i++)
{
if (f[i].count != 0)
{
for (int j = 0; j < f[i].count; j++)
{
cout << f[i].value;
}
}
}
return 0;
}