-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10008.cpp
96 lines (80 loc) · 1.85 KB
/
10008.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <cstdio>
using namespace std;
void sortC(int *cnt, char *arr, int len)
{
char tmpChar;
int i, j, tmpInt;
char pivot = arr[len / 2];
if (len < 2) return;
i = 0;
j = len - 1;
while (true) {
while (arr[i] < pivot)
++i;
while (arr[j] > pivot)
--j;
if (i >= j) break;
tmpInt = cnt[i];
cnt[i] = cnt[j];
cnt[j] = tmpInt;
tmpChar = arr[i];
arr[i++] = arr[j];
arr[j--] = tmpChar;
}
sortC(cnt, arr, i);
sortC(cnt + i, arr + i, len - i);
}
void sortN(int *cnt, char *arr, int len)
{
char tmpChar;
int i, j, tmpInt;
int pivot = cnt[len / 2];
if (len < 2) return;
i = 0;
j = len - 1;
while (true) {
while (cnt[i] > pivot)
++i;
while (cnt[j] < pivot)
--j;
if (i >= j) break;
tmpInt = cnt[i];
cnt[i] = cnt[j];
cnt[j] = tmpInt;
tmpChar = arr[i];
arr[i++] = arr[j];
arr[j--] = tmpChar;
}
sortN(cnt, arr, i);
sortN(cnt + i, arr + i, len - i);
}
int main(void)
{
int cnt[26], i, j;
char c, arr[26];
for (i = 0; i < 26; ++i) {
cnt[i] = 0;
arr[i] = 'A' + i;
}
while (scanf("%c", &c) != EOF) {
if ('a' <= c && c <= 'z')
cnt[c - 'a']++;
else if ('A' <= c && c <= 'Z')
cnt[c - 'A']++;
}
sortN(cnt, arr, 26);
for (i = 0, j = 0; i < 26 && cnt[i] > 0; ++i)
if (cnt[i] != cnt[j]) {
sortC(cnt + j, arr + j, i - j);
for (; j < i; ++j)
printf("%c %d\n", arr[j], cnt[j]);
j = i;
}
if (cnt[j] > 0) {
sortC(cnt + j, arr + j, i - j);
for (; j < i; ++j)
printf("%c %d\n", arr[j], cnt[j]);
j = i;
}
return 0;
}