Advertisement
Hezov

Datorii OJI VIII

Jan 12th, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. #include <map>
  4. using namespace std;
  5. typedef long long ll;
  6. ifstream cin("datorii.in");
  7. ofstream cout("datorii.out");
  8. map<string,pair<ll,ll>> M;
  9. int main()
  10. {
  11.     int c  , d;
  12.     cin >> c >> d; cin.get();
  13.     for(int i = 1;i<=d;i++)
  14.     {
  15.         string datorie;
  16.         getline(cin,datorie);
  17.         int p1 = datorie.length() - 1,p2;
  18.         bool spatiu = false;
  19.         while(datorie[p1] != '>')
  20.         {
  21.             if(isdigit(datorie[p1]) && !spatiu)
  22.                 p2=p1;
  23.             if(datorie[p1] == ' ' )
  24.                 spatiu = true;
  25.             p1--;
  26.         }
  27.         int suma = 0;
  28.         for(int it = p2;it<datorie.length();it++)
  29.             suma = suma * 10 + (datorie[it] - '0');
  30.         string dator = datorie.substr(0,p1-1);
  31.         string primitor = datorie.substr(p1 + 2, p2 - (p1 + 3));
  32.         M[dator].first += suma;
  33.         M[primitor].second += suma;
  34.     }
  35.     if(c==1)
  36.         cout << M.size() << '\n';
  37.     if(c==2)
  38.         for(auto it : M)
  39.             cout << it.first << ' ' << it.second.first << ' ' << it.second.second << '\n';
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement