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

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
typedef string::const_iterator State;
class ParseError {};
string expression(State &);
string ch(State &);
int number(State &);

int m;
string s;
string expression(State &begin) {
  string ret = "", tmp = "";
  int num = 1;
  // cout << *begin << " " << begin - s.begin() << endl;
  if (isdigit(*begin)) {
    num = number(begin);
    // begin++;
  }
  if (*begin == '(') {
    begin++;
    tmp = expression(begin);
    begin++;
  } else if (isupper(*begin)) {
    tmp = ch(begin);
    // begin++;
  }

  // cout << "num:" << num << " tmp:" << tmp << endl;
  REP(i, num) {
    if ((int)ret.size() > m)
      return ret;
    ret += tmp;
  }

  // cout << ret << endl;
  if (begin - s.begin() < (int)s.size() && *begin == ')')
    return ret;
  if (begin - s.begin() < (int)s.size()) {
    // cout << "end" << *begin << endl;
    ret += expression(begin);
  }
  // cout << *begin << " ret:" << ret << endl;
  return ret;
}

//???????????????
string ch(State &begin) {
  string ret;
  while (1) {
    if (isupper(*begin)) {
      ret += *begin;
      begin++;
    } else {
      break;
    }
  }
  // cout << "char" << ret << endl;
  return ret;
}

int number(State &begin) {
  int ret = 0;
  while (isdigit(*begin)) {
    ret *= 10;
    ret += *begin - '0';
    begin++;
  }
  return ret;
}

signed main(void) {
  while (1) {
    cin >> s >> m;
    if (s == "0" && !m)
      break;
    // REP(i, n) REP(j, n)
    State begin = s.begin();
    string ans = expression(begin);
    // cout << "ans:" << ans << endl;
    if ((int)ans.size() <= m)
      cout << "0" << endl;
    else
      cout << ans[m] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

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

int m;
char ans;
bool flag = false;
string dfs(string s) {
  // cout << s << endl;
  if (flag)
    return "";
  if ('A' <= s[0] && s[0] <= 'Z') {
    return s;
  }
  string ret = "";
  // 10C??????????????????????¢????
  /*bool h = false, ff = false;
  string tt = "";
  int idx = -1;
  REP(i, s.size()) {
          if(i >= 5) break;
          if(s[i] == '(') h = true;
          if(isdigit(s[i])) tt += s[i], idx = i, ff = true;
          else if(ff && isdigit(s[i])) h = false;
  }
  string ret = "";
  if(!h) {
          string tmp = dfs(s.substr(idx+1));
          int itt = stoi(tt);
          //cout << "tmp" << tmp << "itt" << itt << endl;
          REP(i, itt) {
                  ret += tmp;
                  //cout << ret << endl;
                  if((int)ret.size() > m) {
                          ans = ret[m];
                          flag = true;
                          //cout << ans << endl;
                          return "";
                  }
          }
          return ret;
  }
  //cout << "b" << endl;
  */
  string num = "", para = "";
  int now = 0;
  bool f = true, g = false, kakko = false;
  REP(i, s.size()) {
    if (s[i] == '(') {
      kakko = true;
      g = true;
      now++;
    } else if (s[i] == ')') {
      now--;
    } else if (isdigit(s[i])) {
      f = false;
    } else {
      f = true;
      g = true;
    }
    if (g)
      para += s[i];
    else
      num += s[i];
    if (now == 0 && f) {
      // tmp???dfs?????????????????°?????????????????????ret????????????
      // cout << "para" << para << "num" << num << endl;
      int ttmp = stoi(num);
      string dret;
      if (kakko)
        dret = dfs(para.substr(1, para.size() - 2));
      else
        dret = dfs(para);
      REP(j, ttmp) {
        ret += dret;
        if ((int)ret.size() > m) {
          ans = ret[m];
          flag = true;
          // cout << ans << endl;
          break;
        }
      }
      if (flag)
        break;
      num = "", para = "";
      f = true, g = false;
    }
  }

  return ret;
}

signed main(void) {
  while (true) {
    string s;
    cin >> s >> m;
    if (s == "0" && !m)
      break;

    flag = false;

    bool f = true;
    int now = 0;
    string tmp = "";
    REP(i, s.size()) {
      // cout << "i:" << i << " " << s[i] << endl;
      if (s[i] == '(')
        now++;
      else if (s[i] == ')')
        now--;
      else if (isdigit(s[i]))
        f = false;
      else
        f = true;
      tmp += s[i];
      // cout << tmp << " " << m << endl;
      if (now == 0 && f) {
        string t = dfs(tmp);
        // cout << "a" << endl;
        if (flag) {
          cout << ans << endl;
          goto e;
        } else if ((int)t.size() <= m) {
          m -= t.size();
        } else {
          cout << t[m] << endl;
          goto e;
        }
        tmp = "";
      }
    }
    cout << 0 << endl;
  e:
    int a;
  }

  return 0;
}