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

#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef pair<P, P> PP;
string s;
int p, n, c = 0;
string bnf1();
string get() {
  string res;
  if (s[p] == '(')
    p++, res = bnf1(), p++;
  else if (isdigit(s[p]) || isalpha(s[p])) {
    while (isdigit(s[p]) || isalpha(s[p])) {
      res += s[p++];
      if (res.size() > 1000000)
        return res.substr(0, 1000009);
    }
  }
  return res;
}
string bnf2() {
  string res = get();
  while (s[p] == '*') {
    if (res.size() > 1000000)
      return res.substr(0, 1000009);
    p++;
    string res2 = get();
    int num = 0;
    r(i, res.size()) num = num * 10 + (res[i] - '0');
    res = res2;
    r(i, num - 1) {
      res += res2;
      if (res.size() > 1000000)
        return res.substr(0, 1000009);
    }
  }
  return res;
}
string bnf1() {
  string res = bnf2();
  while (s[p] == '+') {
    if (res.size() > 1000000)
      return res.substr(0, 1000009);
    p++;
    string res2 = bnf2();
    res += res2;
    if (res.size() > 1000000)
      return res.substr(0, 1000009);
  }
  return res;
}
int main() {
  while (cin >> s >> n, s != "0") {
    p = 0;
    r(i, (int)s.size() - 1) {
      if (isdigit(s[i]) && s[i + 1] == '(' ||
          isdigit(s[i]) && isalpha(s[i + 1])) {
        s = s.substr(0, i + 1) + "*" + s.substr(i + 1);
        i--;
      } else if (s[i] == ')' && isalpha(s[i + 1]) ||
                 s[i] == ')' && isdigit(s[i + 1]) ||
                 isdigit(s[i + 1]) && isalpha(s[i])) {
        s = s.substr(0, i + 1) + "+" + s.substr(i + 1);
        i--;
      }
    }
    string ans = bnf1();
    if (n < ans.size())
      cout << ans[n] << endl;
    else
      cout << 0 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef pair<P, P> PP;
string s;
int p, n;
string bnf1();
string get() {
  string res;
  if (s[p] == '(')
    p++, res = bnf1(), p++;
  else if (isdigit(s[p]) || isalpha(s[p])) {
    while (isdigit(s[p]) || isalpha(s[p])) {
      res += s[p++];
      if (res.size() > 1000000)
        return res;
    }
  }
  return res;
}
string bnf2() {
  string res = get();
  while (s[p] == '*') {
    if (res.size() > 1000000)
      return res;
    p++;
    string res2 = get();
    int num = 0;
    r(i, res.size()) num = num * 10 + (res[i] - '0');
    res = res2;
    r(i, num - 1) {
      res += res2;
      if (res.size() > 1000000)
        return res;
    }
  }
  return res;
}
string bnf1() {
  string res = bnf2();
  while (s[p] == '+') {
    if (res.size() > 1000000)
      return res;
    p++;
    string res2 = bnf2();
    res += res2;
    if (res.size() > 1000000)
      return res;
  }
  return res;
}
int main() {
  while (cin >> s >> n, s != "0") {
    p = 0;
    r(i, (int)s.size() - 1) {
      if (isdigit(s[i]) && s[i + 1] == '(' ||
          isdigit(s[i]) && isalpha(s[i + 1])) {
        s = s.substr(0, i + 1) + "*" + s.substr(i + 1);
        i--;
      } else if (s[i] == ')' && isalpha(s[i + 1]) ||
                 s[i] == ')' && isdigit(s[i + 1])) {
        s = s.substr(0, i + 1) + "+" + s.substr(i + 1);
        i--;
      }
    }
    string ans = bnf1();
    if (n < ans.size())
      cout << ans[n] << endl;
    else
      cout << 0 << endl;
  }
}