UVa10062

題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=1003

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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>

using namespace std;

bool cmp(const pair<int, int> &a, const pair<int, int> &b){
if(a.second == b.second) return a.first > b.first; // second = showed times, if equal, ASCII bigger will put at front
return a.second < b.second; // otherwise the showed times lower will put at front
}

int main(){
string text;
bool first = true;
while(getline(cin, text)){
if(first) first = false;
else cout << '\n';

map<int, int> m;
for(size_t i=0; i<text.length(); ++i){
int num = int(text[i]);
if(num >= 32 && num < 128)
m[num]++; // get the showed times of each element
}
vector<pair<int, int>> freq(m.begin(), m.end()); // make pair of key to showed times
sort(freq.begin(), freq.end(), cmp);
for(auto &p : freq){
cout << p.first << ' ' << p.second << '\n';
}
}
return 0;
}