/** Count words.
One word per line.
Print the most frequent words and their counts first.
To try:
$ g++ -std=c++11 count-words.cpp && ./a.out <input
One 3
Three 1
Two 1
*/
#include <algorithm> // sort
#include <iostream>
#include <unordered_map>
#include <utility> // pair
#include <iterator> // begin
#include <vector>
int main() {
using namespace std;
unordered_map<string, size_t> counter;
string line;
getline(cin, line); // read n and ignore it
while(getline(cin, line)) // count words
++counter[line];
if (!cin.eof())
return 1; // I/O error
// print the most frequent words first
typedef pair<string,size_t> Pair;
vector<Pair> v(begin(counter), end(counter));
sort(begin(v), end(v), [](const Pair& a, const Pair& b) {
return a.second > b.second;
});
for (const auto& p : v)
cout << p.first << " " << p.second << endl;
}
LyoqIENvdW50IHdvcmRzLgoKICAgIE9uZSB3b3JkIHBlciBsaW5lLgogICAgUHJpbnQgdGhlIG1vc3QgZnJlcXVlbnQgd29yZHMgYW5kIHRoZWlyIGNvdW50cyBmaXJzdC4KCiAgICBUbyB0cnk6CgogICAgICAkIGcrKyAtc3RkPWMrKzExIGNvdW50LXdvcmRzLmNwcCAmJiAuL2Eub3V0IDxpbnB1dAogICAgICBPbmUgMwogICAgICBUaHJlZSAxCiAgICAgIFR3byAxCgoqLwojaW5jbHVkZSA8YWxnb3JpdGhtPiAvLyBzb3J0CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHVub3JkZXJlZF9tYXA+CiNpbmNsdWRlIDx1dGlsaXR5PiAvLyBwYWlyCiNpbmNsdWRlIDxpdGVyYXRvcj4gLy8gYmVnaW4KI2luY2x1ZGUgPHZlY3Rvcj4KCmludCBtYWluKCkgewogICAgdXNpbmcgbmFtZXNwYWNlIHN0ZDsKICAgIHVub3JkZXJlZF9tYXA8c3RyaW5nLCBzaXplX3Q+IGNvdW50ZXI7CiAgICBzdHJpbmcgbGluZTsKICAgIGdldGxpbmUoY2luLCBsaW5lKTsgLy8gcmVhZCBuIGFuZCBpZ25vcmUgaXQKICAgIHdoaWxlKGdldGxpbmUoY2luLCBsaW5lKSk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