## UVA 10062 - Tell me the frequencies!
### 題意:
###### 列出每個case裡面的字元 (用ASCII code表示),出現次數由小到大,ASCII code由大到小排序。
### Sample Input:
###### 每一行(最長length為1000)視為一個test case,輸入直到EOF
```=
AAABBC
122333
```
### Sample Output:
###### 排序從出現次數少到多。
###### 如果出現次數一樣,ASCII code由大到小排序。
###### 每個case的輸出要用一個blank line分隔開。
```=
67 1
66 2
65 3
49 1
50 2
51 3
```
### 程式碼:
```cpp=
#include <iostream>
#include <string>
#include <map>
#define haku author
using namespace std;
int main() {
string str; int c = 0;
while (getline(cin, str)) {
if (c != 0) cout << endl;
map<int, int> mp; //我真的不想想變數名了
for (int i = 0; i < str.size(); i++) {
if (mp.find(int(str[i])) != mp.end()) mp[int(str[i])]++;
else mp.insert({ int(str[i]), 1 });
}
int max = str.size();
for (int i = 1; i <= str.size(); i++) {
for (auto p = mp.rbegin(); p != mp.rend(); p++) {
if (p->second == i) cout << p->first << ' ' << p->second << endl;
}
}
c++;
}
return 0;
}
```
程式碼更新: 使用 cmp 排序
```cpp=
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#define cmp haku
using namespace std;
bool cmp(pair<char, int> a, pair<char, int> b) {
if (a.second != b.second) return a.second < b.second;
return a.first > b.first;
}
int main() {
string line; bool start = false;
while (getline(cin, line)) {
map<char, int> mp;
for (auto& c : line) {
mp[c]++;
}
vector<pair<char, int>> v(mp.begin(), mp.end());
sort(v.begin(), v.end(), cmp);
if (start) cout << endl;
else start = true;
for (auto& p : v) {
cout << int(p.first) << ' ' << p.second << endl;
}
}
return 0;
}
```