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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

using pii = pair<int, int>;
using ll = long long;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define repeat(i, j, k) for (int i = (j); i < (int)(k); i++)
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << " : " << x << endl

template <class T> bool set_min(T &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <class T> bool set_max(T &a, const T &b) {
  return a < b ? a = b, true : false;
}
// vector
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &a : v)
    is >> a;
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v)
    os << "\\t" << t;
  return os << endl;
}
// pair
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
  return os << "<" << v.first << ", " << v.second << ">";
}

const int INF = 1 << 30;
const ll INFL = 1LL << 60;

int get_int(const string &S, int &pos) {
  int beg = pos;
  for (; isdigit(S[pos]); pos++)
    ;
  return stoi(S.substr(beg, pos - beg + 1));
}

class Solver {
public:
  string S, buf;
  int pos, N;

  void solve2() {
    // cerr << "-> " << S.substr(0, pos + 1) << endl;
    bool paren = S[pos] == '(';
    if (paren)
      pos++;
    do {
      if (isdigit(S[pos])) {
        int rec = get_int(S, pos);
        if (S[pos] == '(') {
          int now = pos;
          rep(i, rec) {
            pos = now;
            solve2();
            if (buf.size() > N)
              return;
          }
        } else {
          rep(i, rec) {
            buf.push_back(S[pos]);
            if (buf.size() > N)
              return;
          }
          pos++;
        }
      } else if (isalpha(S[pos])) {
        do {
          buf.push_back(S[pos++]);
        } while (paren and isalpha(S[pos]) and buf.size() <= N);
      } else
        assert(0);
    } while (paren and S[pos] != ')' and buf.size() <= N);
    // cerr << "<- " << S.substr(0, pos + 1) << endl;
    if (paren)
      pos++;
  }

  bool solve() {
    cin >> S >> N;
    if (S == "0" and N == 0)
      return false;
    pos = 0;
    buf.reserve(N * 2);
    while (buf.size() <= N and pos < S.size())
      solve2();

    cout << (buf.size() <= N ? "0" : buf.substr(N, 1)) << endl;
    return true;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int i = 0;
  while (true) {
    Solver s;
    if (not s.solve())
      break;
  }
  return 0;
}

Error: WA

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

using pii = pair<int, int>;
using ll = long long;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define repeat(i, j, k) for (int i = (j); i < (int)(k); i++)
#define all(v) v.begin(), v.end()
// #define debug(x) cerr << #x << " : " << x << endl
#define debug(...)

template <class T> bool set_min(T &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <class T> bool set_max(T &a, const T &b) {
  return a < b ? a = b, true : false;
}
// vector
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &a : v)
    is >> a;
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v)
    os << "\\t" << t;
  return os << endl;
}
// pair
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
  return os << "<" << v.first << ", " << v.second << ">";
}

const int INF = 1 << 30;
const ll INFL = 1LL << 60;

string get_foo(const string &S, int &pos, function<int(int)> isfoo) {
  int len = 0;
  for (; isfoo(S[pos + len]); len++)
    ;
  string ret = S.substr(pos, len);
  pos += len;
  return ret;
}
string calc(const string &S, int &pos, int N) {
  // cerr << "-> " << S.substr(0, pos + 1) << endl;
  bool paren = S[pos] == '(';
  if (paren)
    pos++;
  string res;
  res.reserve(N + 1);
  do {
    if (isdigit(S[pos])) {
      int rec = stoi(get_foo(S, pos, [](int a) { return isdigit(a); }));
      int need = N - res.size();
      string ss = calc(S, pos, need);
      rep(i, rec) {
        rep(j, ss.size()) {
          res.push_back(ss[j]);
          if (res.size() >= N)
            goto A;
        }
      }
    A:;
    } else if (isalpha(S[pos])) {
      if (paren) {
        res.append(get_foo(S, pos, [](int a) { return isalpha(a); }));
      } else {
        res.push_back(S[pos++]);
      }
    } else {
      debug(pos);
      debug(S[pos]);
      assert(0);
    }
  } while (paren and S[pos] != ')' and res.size() < N);
  // cerr << "<- " << S.substr(0, pos + 1) << " " << (paren ? "!": "") <<endl;
  if (paren)
    pos++;
  return res;
}

char res[1000000 + 5];

bool solve() {
  string S;
  cin >> S;
  int N;
  cin >> N;
  if (S == "0" and N == 0)
    return false;
  cerr << S << " " << N << endl;
  int pos = 0, pos2 = 0;
  while (pos < S.size() and pos2 <= N) {
    string a = calc(S, pos, N - pos2 + 1);
    rep(i, a.size()) {
      res[pos2++] = a[i];
      if (pos2 > N)
        break;
    }
  }
  cout << (pos2 <= N ? "0" : string(res[N], 1)) << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int i = 0;
  while (true) {
    if (not solve())
      break;
  }
  return 0;
}