Advertisement
WallHero

Nombres repetidos.

Nov 21st, 2020
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     int C, N;
  6.     cin >> C >> N;
  7.     map<string, int> nombres;
  8.     string aux;
  9.     for(int i = 0; i < C; i++)
  10.     {
  11.         cin >> aux;
  12.         nombres[aux]++;
  13.     }
  14.     vector<int> cantidades; // Las veces que aparece X palabra
  15.     for(auto e : nombres)
  16.     {
  17.         cantidades.push_back(e.second);
  18.     }
  19.     sort(cantidades.begin(), cantidades.end(), greater<int>());
  20.     cantidades.resize(N);
  21.    
  22.     for(auto e : nombres)
  23.     {
  24.         if(find(cantidades.begin(), cantidades.end(), e.second) != cantidades.end())
  25.         {
  26.            cout << e.first << " " << e.second << "\n";
  27.         }
  28.     }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement