Advertisement
STANAANDREY

AOCMMXX d7p2

Dec 13th, 2020
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int NMAX = 600;
  4. const string TARGET = "shinygold";
  5. map<string, int> colToNum;
  6. #define fi first
  7. #define se second
  8. vector<pair<int, int>> edges[NMAX];
  9. int nrcol, ans, start;
  10.  
  11. bool aBag(string s) {
  12.     return s == "bag" || s == "bags";
  13. }
  14.  
  15. vector<string> strSplit(string s, string sep) {
  16.     const size_t n = s.size();
  17.     vector<string> splited;
  18.     int st = 0, fin = n - 1;
  19.     for (size_t i = 0; i <= n; i++) {
  20.         if (sep.find(s[i]) != string::npos || i == n) {
  21.             fin = i;
  22.             string temp = string(s.begin() + st, s.begin() + fin);
  23.             if (!temp.empty())
  24.                 splited.push_back(temp);
  25.             st = i + 1;
  26.         }
  27.     }
  28.     return splited;
  29. }
  30.  
  31. void addEdge(string from, string to, int nr) {
  32.     if (!colToNum.count(from))
  33.         colToNum[from] = ++nrcol;
  34.     if (from == TARGET)
  35.         start = colToNum[from];//TARGET id
  36.     if (!colToNum.count(to))
  37.         colToNum[to] = ++nrcol;
  38.     edges[colToNum[from]].push_back(make_pair(colToNum[to], nr));
  39. }
  40.  
  41. uint64_t dfs(int node) {
  42.     ans++;
  43.     uint64_t r = 0;
  44.     for (auto it : edges[node]) {
  45.         r += dfs(it.fi) * it.se;
  46.     }
  47.     return r + 1;
  48. }
  49.  
  50. int main() {
  51.     freopen("text.in", "r", stdin);
  52.     string s;
  53.     while (getline(cin, s)) {
  54.         vector<string> splited = strSplit(s, ",. ");
  55.         if (s.empty() || find(splited.begin(), splited.end(), "other") != splited.end())
  56.             continue;
  57.         int it = 0;
  58.         string fromCol;
  59.         while (!aBag(splited[it]))
  60.             fromCol += splited[it++];
  61.         it += 2;
  62.         //cerr << "FROM:" << fromCol << endl;
  63.         string toCol;
  64.         int nr;
  65.         for (; it < (int)splited.size(); it++) {
  66.             if (isdigit(splited[it][0])) {
  67.                 nr = atoi(splited[it].c_str());
  68.                 continue;
  69.             }
  70.             if (!aBag(splited[it])) {
  71.                 toCol += splited[it];
  72.             } else {
  73.                 //cerr << "TO:" << toCol << "NR:" << nr << endl;
  74.                 addEdge(fromCol, toCol, nr);
  75.                 toCol = "";
  76.             }
  77.         }
  78.     }//*/
  79.     cout << dfs(start) - 1 << endl;
  80.     return 0;
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement