Problem p00726 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define reps(i, j, k) for (int i = j; i < k; i++)
#define rep(i, j) reps(i, 0, j)
#define InumF 1 << 30
#define fr first
#define sc second
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
typedef pair<int, int> Pii;

int main() {
  string str;
  int n;
  while (true) {
    cin >> str;
    scanf("%d", &n);
    if (str == "0" && n == 0)
      break;
    int len = str.size();
    string numew = "";
    numew += str[0];
    reps(i, 1, len) {
      if (isdigit(str[i - 1]) && isalpha(str[i])) {
        numew += "(";
        numew += str[i];
        numew += ")";
      } else {
        numew += str[i];
      }
    }
    str = numew;
    stack<int> S;
    vector<int> par(128, -1);
    vector<int> num(128, -1);
    rep(i, str.size()) {
      if (isdigit(str[i])) {
        string tmp = "";
        int j;
        for (j = i; str[j] != '('; j++) {
          tmp += str[j];
        }
        num[i] = atoi(tmp.c_str());
        i = j;
      }
    }

    rep(i, str.size()) {
      if (str[i] == '(') {
        for (int j = i - 1; j >= 0; j--) {
          if (num[j] != -1) {
            S.push(j);
            break;
          }
        }
      }
      if (str[i] == ')') {
        par[i] = S.top();
        S.pop();
      }
      // cout << num[i] << " ";
    }

    int now = 0;
    int cnt = 0;
    vector<int> nex = num;

    while (true) {
      // cout<<now<<"("<<cnt<<")"<<" ";
      if (isalpha(str[now])) {
        // cout<<str[now];
        if (cnt == n) {
          cout << str[now] << endl;
          break;
        }
        now++, cnt++;
      } else if (isdigit(str[now])) {
        nex[now]--;
        if (nex[now] == 0)
          nex[now] = num[now];
        int j = now;
        while (true) {
          if (str[j] == '(')
            break;
          j++;
        }
        j++;
        now = j;
      } else if (str[now] == '(')
        now++;
      else if (str[now] == ')') {
        if (nex[par[now]] == num[par[now]])
          now++;
        else
          now = par[now];
      }
      if (now == str.size()) {
        cout << 0 << endl;
        break;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define reps(i, j, k) for (int i = j; i < k; i++)
#define rep(i, j) reps(i, 0, j)
#define InumF 1 << 30
#define fr first
#define sc second
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
typedef pair<int, int> Pii;

int main() {
  string str;
  int n;
  while (true) {
    cin >> str;
    scanf("%d", &n);
    if (str == "0" && n == 0)
      break;
    int len = str.size();
    string numew = "";
    numew += str[0];
    reps(i, 1, len) {
      if (isdigit(str[i - 1]) && isalpha(str[i])) {
        numew += "(";
        numew += str[i];
        numew += ")";
      } else {
        numew += str[i];
      }
    }
    str = numew;
    stack<int> S;
    vector<int> par(1280, -1);
    vector<int> num(1280, -1);
    rep(i, str.size()) {
      if (isdigit(str[i])) {
        string tmp = "";
        int j;
        for (j = i; str[j] != '('; j++) {
          tmp += str[j];
        }
        num[i] = atoi(tmp.c_str());
        i = j;
      }
    }

    rep(i, str.size()) {
      if (str[i] == '(') {
        for (int j = i - 1; j >= 0; j--) {
          if (num[j] != -1) {
            S.push(j);
            break;
          }
        }
      }
      if (str[i] == ')') {
        par[i] = S.top();
        S.pop();
      }
      // cout << num[i] << " ";
    }

    int now = 0;
    int cnt = 0;
    vector<int> nex = num;

    while (true) {
      // cout<<now<<"("<<cnt<<")"<<" ";
      if (isalpha(str[now])) {
        // cout<<str[now];
        if (cnt == n) {
          cout << str[now] << endl;
          break;
        }
        now++, cnt++;
      } else if (isdigit(str[now])) {
        nex[now]--;
        if (nex[now] == 0)
          nex[now] = num[now];
        int j = now;
        while (true) {
          if (str[j] == '(')
            break;
          j++;
        }
        j++;
        now = j;
      } else if (str[now] == '(')
        now++;
      else if (str[now] == ')') {
        if (nex[par[now]] == num[par[now]])
          now++;
        else
          now = par[now];
      }
      if (now == str.size()) {
        cout << 0 << endl;
        break;
      }
    }
  }
  return 0;
}