Problem p00726 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;

struct Node {
  ll size;
  ll repeat;
  string s;
  vector<Node> v;
  Node() {
    size = 0;
    repeat = 0;
    s = "";
  }
  Node(ll size, ll repeat, string s) {
    this->size = size;
    this->repeat = repeat;
    this->s = s;
  }
};

ll fetch(const string &s, ll index) {
  ll po = 1;
  for (ll i = index + 1; i < s.size(); i++) {
    if (s[i] == '(')
      po++;
    else if (s[i] == ')')
      po--;
    if (!po)
      return i;
  }
}

Node dfs(const string &s) {
  ll num = 0;
  string t = "";
  Node ret(0, 1, "");
  auto f = [&] {
    if (t != "") {
      ret.v.push_back(Node(t.size(), 1, t));
      t = "";
    }
  };

  for (ll i = 0; i < s.size(); i++) {
    if ('0' <= s[i] && s[i] <= '9') {
      f();
      num = 10 * num + s[i] - '0';
    } else if ('A' <= s[i] && s[i] <= 'Z') {
      for (ll j = 0; j < max<ll>(1, num); j++)
        t += s[i];
      num = 0;
    } else if (s[i] == '(') {
      f();
      ll fe = fetch(s, i);
      Node tmp = dfs(s.substr(i + 1, (fe - 1) - (i + 1) + 1));
      tmp.repeat = max<ll>(1, num);
      i = fe;
      num = 0;
      ret.v.push_back(tmp);
    }
  }
  if (t != "")
    f();
  for (auto child : ret.v) {
    ret.size += child.size * child.repeat;
    if (ret.size > 1000002)
      ret.size = 1000002;
  }
  if (ret.size < 1000002)
    ret.size = ret.repeat * (ret.size + ret.s.size());
  return ret;
}

char solve(const Node &n, ll i) {
  if (n.s.size() > i)
    return n.s[i];
  for (auto child : n.v) {
    if (child.size * child.repeat <= i) {
      i -= child.size * child.repeat;
    } else {
      return solve(child, i % child.size);
    }
  }
  // return n.s[i];
}

int main() {
  while (1) {
    string S;
    ll I;
    cin >> S >> I;
    if (S == "0" && !I)
      break;
    S = "(" + S + ")";
    auto node = dfs(S);
    if (node.size <= I)
      cout << 0 << endl;
    else
      cout << solve(node, I) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
bool out;
bool output(string s, ll I, ll mult, bool f) {
  if (!f && s.size() * mult < I)
    return false;
  out = 1;
  I %= s.size();
  cout << s[I] << endl;
  return true;
}
int main() {
  while (1) {
    string s;
    ll I;
    cin >> s >> I;
    if (s == "0" && !I)
      break;
    stack<ll> st;
    string now = "";
    ll num = 0;
    ll mult = 1;
    bool f = 0;
    for (char c : s) {
      if ('0' <= c && c <= '9') {
        num = num * 10 + (c - '0');
      } else if (c == '(') {
        st.push(num);
        mult *= num;
        num = 0;
        if (mult >= I) {
          mult = 1;
          f = 1;
        }
      } else if ('A' <= c && c <= 'Z') {
        for (ll i = 0; i < max<ll>(1, num); i++)
          now += c;
      } else if (c == ')') {
        ll tmp = st.top();
        st.pop();
        if (output(now, I, mult, f)) {
          break;
        } else {
          I -= now.size() * mult;
          now = "";
        }
        mult /= tmp;
      }
    }
    if (!out) {
      if (I < now.size())
        cout << now[I] << endl;
      else
        cout << 0 << endl;
    }
    out = 0;
  }
  return 0;
}