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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;
typedef stringstream SS;

struct Edge {
  int to, from, cost;
};

#ifdef DEBUG
#define debug cout
#else
SS ss;
#define debug ss
#endif

template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repnd(i, m, n) for (int i = (n); i >= (m); --i)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define repn0(i, m, n) for (i = (m); i <= (n); ++i)
#define repd0(i, n) for (i = (n)-1; i >= 0; --i)
#define repnd0(i, m, n) for (i = (n); i >= (m); --i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define X second
#define Y first
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

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

Int g(string s) {
  Int p;
  rep0(p, sz(s)) if (isdigit(s[p])) break;
  if (p == sz(s))
    return sz(s);
  Int c = 0, q, r, times = 0;
  repn0(q, p, sz(s) - 1) {
    if (!isdigit(s[q]))
      break;
    times = times * 10 + s[q] - '0';
  }
  if (s[q] != '(') {
    s = s.substr(0, q) + "(" + s.substr(q, 1) + ")" + s.substr(q + 1);
  }
  rep0(r, sz(s)) {
    if (s[r] == '(')
      ++c;
    if (s[r] == ')') {
      --c;
      if (c == 0)
        break;
    }
  }
  return min(1000001LL,
             p + times * g(s.substr(q + 1, r - q - 1)) + g(s.substr(r + 1)));
}

char f(string s, Int i) {
  // cout<<s<<" "<<i<<endl;
  Int p;
  rep0(p, sz(s)) if (isdigit(s[p])) break;
  if (p == sz(s)) {
    if (i < p)
      return s[i];
    else
      return '0';
  }
  if (i < p)
    return s[i];
  s = s.substr(p);
  i -= p;
  Int c = 0, q, times = 0;
  rep0(p, sz(s)) {
    if (!isdigit(s[p]))
      break;
    times = times * 10 + s[p] - '0';
  }
  if (s[p] != '(') {
    s = s.substr(0, p) + "(" + s.substr(p, 1) + ")" + s.substr(p + 1);
  }
  rep0(q, sz(s)) {
    if (s[q] == '(')
      ++c;
    if (s[q] == ')') {
      --c;
      if (c == 0)
        break;
    }
  }
  Int len = g(s.substr(p + 1, q - p - 1));
  if (len * times > i) {
    i %= len;
    return f(s.substr(p + 1, q - p - 1), i);
  } else {
    i -= len * times;
    return f(s.substr(q + 1), i);
  }
}

int main() {
  string s;
  Int i;
  IL {
    cin >> s >> i;
    if (s == "0" && i == 0)
      break;
    cout << f(s, i) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;
typedef stringstream SS;

struct Edge {
  int to, from, cost;
};

#ifdef DEBUG
#define debug cout
#else
SS ss;
#define debug ss
#endif

template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repnd(i, m, n) for (int i = (n); i >= (m); --i)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define repn0(i, m, n) for (i = (m); i <= (n); ++i)
#define repd0(i, n) for (i = (n)-1; i >= 0; --i)
#define repnd0(i, m, n) for (i = (n); i >= (m); --i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define X second
#define Y first
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

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

Int g(string s) {
  Int p;
  rep0(p, sz(s)) if (isdigit(s[p])) break;
  if (p == sz(s))
    return sz(s);
  Int c = 0, q, r, times = 0;
  repn0(q, p, sz(s) - 1) {
    if (!isdigit(s[q]))
      break;
    times = times * 10 + s[q] - '0';
  }
  rep0(r, sz(s)) {
    if (s[r] == '(')
      ++c;
    if (s[r] == ')') {
      --c;
      if (c == 0)
        break;
    }
  }
  return min(1000000LL,
             p + times * g(s.substr(q + 1, r - q - 1)) + g(s.substr(r + 1)));
}

char f(string s, Int i) {
  Int p;
  rep0(p, sz(s)) if (isdigit(s[p])) break;
  if (p == sz(s)) {
    if (i < p)
      return s[i];
    else
      return '0';
  }
  if (i < p)
    return s[i];
  s = s.substr(p);
  i -= p;
  Int c = 0, q, times = 0;
  rep0(p, sz(s)) {
    if (!isdigit(s[p]))
      break;
    times = times * 10 + s[p] - '0';
  }
  if (s[p] != '(') {
    s = s.substr(0, p) + "(" + s.substr(p, 1) + ")" + s.substr(p + 1);
  }
  rep0(q, sz(s)) {
    if (s[q] == '(')
      ++c;
    if (s[q] == ')') {
      --c;
      if (c == 0)
        break;
    }
  }
  Int len = g(s.substr(p + 1, q - p - 1));
  if (len * times > i) {
    i %= len;
    return f(s.substr(p + 1, q - p - 1), i);
  } else {
    i -= len * times;
    return f(s.substr(q + 1), i);
  }
}

int main() {
  string s;
  Int i;
  IL {
    cin >> s >> i;
    if (s == "0" && i == 0)
      break;
    cout << f(s, i) << endl;
  }
  return 0;
}