fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <utility>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. struct sort_names {
  10. bool operator()(const std::pair<string, int> &left, const std::pair<string, int> &right) {
  11. return left.first < right.first;
  12. }
  13. };
  14.  
  15. struct sort_values {
  16. bool operator()(const std::pair<string, int> &left, const std::pair<string, int> &right) {
  17. return left.second > right.second;
  18. }
  19. };
  20.  
  21. void show(const vector< pair<string, int> > names) {
  22. for(int i = 0; i < names.size(); ++i)
  23. cout << names[i].first << " " << names[i].second << endl;
  24. }
  25.  
  26. int main() {
  27. // string ciag, imie;
  28. string ciag, a, b, imie;
  29. bool flag = true;
  30. int end = 0;
  31. vector< pair<string, int> > names; // vector na ktorym operaujemy
  32.  
  33. // while(getline(cin, ciag)) {
  34. while(cin >> a >> b >> imie) {
  35. // transform(ciag.begin(), ciag.end(), ciag.begin(), ::toupper); // wszystko do duzych liter
  36. transform(imie.begin(), imie.end(), imie.begin(), ::toupper); // imie do duzej litery
  37. // imie = ciag.substr(ciag.find_last_of(" ") + 1, ciag.length()); // wyciagniecie podciagu (imienia)
  38.  
  39. for(int i = 0; i < names.size(); ++i) { // przeszukaj caly vector w poszukiwaniu podobnego imienia
  40. if(names[i].first == imie) {
  41. names[i].second++;
  42. flag = false; // jesli znaleziono to zwieksz jego ilosc
  43. }
  44. }
  45.  
  46. if(flag) { // jesli nie znaleziono tych samych imion to dodajemy nowe imie
  47. names.push_back(std::make_pair(imie, 1));
  48. flag = true;
  49.  
  50. }
  51.  
  52. // czy to trzeba rozdzielic na dwa sortowania ?
  53. sort(names.begin(), names.end(), sort_names()); // sortowanie vectora po imionach
  54. sort(names.begin(), names.end(), sort_values()); // sortowanie vectora po values
  55. //show(names); // pokazuje vector / czy musi byc w tej petli czy poza petla ?
  56.  
  57. flag = true; // reset flagi
  58.  
  59. ++end;
  60. if(end >= 7) break;
  61. }
  62.  
  63. show(names);
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0s 3444KB
stdin
1. KowalSki JaCEk
2. mazurkiewicz pIoTR
3. prokoP ANna
4. MisioL annA
5. BerezOwSki jaCEK
6. pietraS ANNA
7. WILkowsKA aneta
stdout
ANNA 3
JACEK 2
ANETA 1
PIOTR 1