fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <unordered_map>
  5. using namespace std;
  6.  
  7. unordered_map <string, unsigned> uniqueWords;
  8.  
  9. bool compare(string a, string b) {
  10. return uniqueWords[a] > uniqueWords[b];
  11. }
  12.  
  13. int main() {
  14. vector<string> words;
  15. for (string s; cin >> s;) {
  16. words.push_back(s);
  17. ++uniqueWords[s];
  18. }
  19. sort(words.begin(), words.end(), compare);
  20. for (auto c : words) {
  21. cout << c << ' ';
  22. }
  23. }
Success #stdin #stdout 0.01s 5436KB
stdin
A fly flies but flies fly too
stdout
fly flies flies fly A but too