File size: 1,190 Bytes
c4b0eef |
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 |
/*
排序
時間複雜度: O(NlogN)
*/
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Count {
private:
int alphabet, counter;
public:
static int now;
Count() {
alphabet = ++now;
counter = 0;
}
void operator++() {
++counter;
}
bool operator<(const Count& other) const {
if (this->counter == other.counter)
return this->alphabet > other.alphabet;
return this->counter < other.counter;
}
friend ostream& operator<<(ostream& out, const Count& count) {
if (count.counter)
out << count.alphabet << " " << count.counter << "\n";
return out;
}
};
int Count::now = -1;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
string str;
bool flag = false;
while (getline(cin, str)) {
if (flag)
cout << "\n";
vector<Count> counter(129);
for (const char& c : str)
++counter[c];
sort(counter.begin(), counter.end());
for (Count i : counter)
cout << i;
Count::now = -1;
flag = true;
}
} |