Advertisement
Guest User

Untitled

a guest
Jan 30th, 2021
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.   vector<pair<string, int>> result;
  10.   string input;
  11.   cin >> input;
  12.   int number = INT_MIN;
  13.   string metal = "";
  14.   while (input != "stop") {
  15.     if (input[0] >= '1' && input[0] <= '9') {
  16.       number = stoi(input);
  17.     } else {
  18.       metal = input;
  19.     }
  20.     if (metal.length() > 0 && number != INT_MIN) {
  21.       auto it = find_if(result.begin(), result.end(), [&](const pair<string, int>& p) { return p.first == metal; });
  22.       if (it != result.end()) {
  23.         it->second += number;
  24.       } else {
  25.         result.emplace_back(metal, number);
  26.       }
  27.       number = INT_MIN;
  28.       metal = "";
  29.     }
  30.     cin >> input;
  31.   }
  32.   for (auto& it : result) {
  33.     cout << it.first << " -> " << it.second << endl;
  34.   }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement