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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

typedef long long ll;
typedef unsigned long long ull;
typedef string str;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vd> vvd;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

char ans;
void saiki(string s, int &out, int &idx) {
  int num = 0;
  while (idx < s.length()) {
    if (isdigit(s[idx])) {
      num *= 10;
      num += s[idx] - '0';
      idx++;
    } else if (s[idx] == '(') {
      idx++;
      int idx2;
      REP(i, num) {
        idx2 = idx;
        saiki(s, out, idx2);
        if (ans != '0' || out < 0)
          return;
      }
      idx = idx2;
      num = 0;
    } else if (s[idx] == ')') {
      idx++;
      return;
    } else {
      string s2 = "";
      if (num == 0) {
        while ('A' <= s[idx] && s[idx] <= 'Z') {
          s2 += s[idx];
          idx++;
        }
      } else {
        s2 += s[idx];
        idx++;
      }
      REP(i, max(1, num))
      if (out < s2.length()) {
        ans = s2[out];
        return;
      } else {
        out -= s2.length();
        if (out < 0)
          return;
      }
      num = 0;
    }
  }
  return;
}
int main() {
  string s;
  int out;
  while (cin >> s >> out, s != "0") {
    int idx = 0;
    ans = '0';
    saiki(s, out, idx);
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int n;
int len;
char loop(string s) {
  int num = 0;
  string ss = "";
  REP(i, s.length()) {
    if (isdigit(s[i])) {
      num *= 10;
      num += s[i] - '0';
    }

    if (s[i] == '(') {
      char c;
      REP(j, num) {
        c = loop(s.substr(i + 1));
        if (c != '0')
          return c;
      }
      num = 0;
      int nu = 0;
      while (1) {
        if (s[i] == ')')
          nu--;
        if (s[i] == '(')
          nu++;
        if (nu == 0)
          break;
        i++;
      }
      continue;
    }
    if (s[i] == ')') {
      if (n - len < ss.length()) {

        return ss[(n - len)];
      } else {

        len += ss.length();
        return '0';
      }
    }
    if ('A' <= s[i] && s[i] <= 'Z') {
      REP(j, num)
      ss += s[i];
      if (num == 0)
        ss += s[i];
    }
  }

  if (n - len < ss.length()) {
    return ss[(n - len)];
  } else {

    len += ss.length();
    return '0';
  }
}

int main() {
  string s;
  while (cin >> s >> n, s != "0") {
    len = 0;

    cout << loop(s) << endl;
  }
}