ulvis.paste.net

Paste Search Dynamic
Recent pastes
WordCount
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <unordered_map>
  4. #include <vector>
  5. using namespace std;
  6. typedef unordered_map<string, int> WordCount;
  7.  
  8. struct Greater{
  9.   bool operator()(const pair<int, WordCount::iterator>& lhs,
  10.                   const pair<int, WordCount::iterator>& rhs) const
  11.   {
  12.     return lhs.second->first < rhs.second->first;
  13.   }
  14. };
  15.  
  16. int main(){
  17.   WordCount counts;
  18.   string word;
  19.   while (cin >> word){
  20.         if(word[0] < 'A' || word[0] > 'Z') break;
  21.     counts[word]++;
  22.   }
  23.   typedef vector<pair<int, WordCount::iterator> > FreqList;
  24.   FreqList freq;
  25.   freq.reserve(counts.size());
  26.   for (WordCount::iterator it = counts.begin(); it != counts.end(); ++it)
  27.     freq.push_back(make_pair(it->second, it));
  28.  
  29.   sort(freq.begin(), freq.end(), Greater());
  30.   cout << freq.size() << '\n';
  31.   for (auto item : freq)
  32.   {
  33.     cout << item.second->first << " " << item.first << '\n';
  34.   }
  35. }
Parsed in 0.007 seconds