fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. int n;
  10. cin >> n;
  11. cin.ignore();
  12.  
  13. vector<pair<string, int>> inventory;
  14.  
  15. for (int i = 0; i < n; ++i) {
  16. string line;
  17. getline(cin, line);
  18. stringstream ss(line);
  19. string item_code;
  20. while (ss >> item_code) {
  21. bool found = false;
  22. for (auto& item : inventory) {
  23. if (item.first == item_code) {
  24. item.second++;
  25. found = true;
  26. break;
  27. }
  28. }
  29. if (!found) {
  30. inventory.push_back({item_code, 1});
  31. }
  32. }
  33. }
  34.  
  35. int k = inventory.size();
  36. for (int i = 0; i < k - 1; ++i) {
  37. for (int j = 0; j < k - i - 1; ++j) {
  38. if (inventory[j].second < inventory[j + 1].second) {
  39. swap(inventory[j], inventory[j + 1]);
  40. } else if (inventory[j].second == inventory[j + 1].second && inventory[j].first > inventory[j + 1].first) {
  41. swap(inventory[j], inventory[j + 1]);
  42. }
  43. }
  44. }
  45.  
  46. for (const auto& item : inventory) {
  47. cout << item.first << " " << item.second << endl;
  48. }
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 5288KB
stdin
10

123

123444

123444

12

1

8

1

455

455

9
stdout
123444 2
1 1
12 1
123 1