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

#include <bits/stdc++.h>
using namespace std;
struct data {
  int num, l, r;
};
vector<data> At, Bt;

bool check(int s, int t, string &str) {
  stack<char> st;
  for (int i = s; i <= t; i++) {
    if (str[i] == '(')
      st.push(str[i]);
    if (str[i] == ')' && st.empty())
      return 0;
    if (str[i] == ')')
      st.pop();
  }
  return st.empty();
}

int get_num(string &str, int &idx) {
  int res = 0;
  while (isdigit(str[idx]))
    res = res * 10 + str[idx++] - '0';
  return res;
}

int get_root(int s, int t, string &str) {
  stack<char> st;
  for (int i = s; i <= t; i++) {
    if (st.empty() && isdigit(str[i]))
      return i;
    if (str[i] == '(')
      st.push(str[i]);
    if (str[i] == ')')
      st.pop();
  }
  assert(0);
}

void make_tree(int s, int t, vector<data> &tree, string &str, int idx) {
  while (s < t && check(s, t, str))
    s++, t--;
  if (s >= t)
    return;
  s--, t++;

  int root = get_root(s, t, str);
  tree.push_back((data){-1, -1, -1});
  tree[idx].l = tree.size() - 1;
  make_tree(s, root - 2, tree, str, tree.size() - 1);

  tree[idx].num = get_num(str, root);

  tree.push_back((data){-1, -1, -1});
  tree[idx].r = tree.size() - 1;
  make_tree(root + 1, t, tree, str, tree.size() - 1);
}

string make_str(int a, int b) {
  if (a == -1 || b == -1 || At[a].num < 0 || Bt[b].num < 0)
    return "";
  int num = At[a].num + Bt[b].num;
  string str;
  if (num == 0)
    str = "0";
  while (num)
    str = (char)((num % 10) + '0') + str, num /= 10;

  return "(" + make_str(At[a].l, Bt[b].l) + ")" + "[" + str + "]" + "(" +
         make_str(At[a].r, Bt[b].r) + ")";
}
int main() {
  string A, B;
  cin >> A >> B;
  At.push_back((data){-1, -1, -1});
  Bt.push_back((data){-1, -1, -1});
  make_tree(0, A.size() - 1, At, A, 0);
  make_tree(0, B.size() - 1, Bt, B, 0);
  cout << make_str(0, 0) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 10000000
using namespace std;
int AT[N], BT[N];

bool check(int s, int t, string &str) {
  stack<char> st;
  for (int i = s; i <= t; i++) {
    if (str[i] == '(')
      st.push(str[i]);
    if (str[i] == ')' && st.empty())
      return 0;
    if (str[i] == ')')
      st.pop();
  }
  return st.empty();
}

int get_num(string &str, int &idx) {
  int res = 0;
  while (isdigit(str[idx]))
    res = res * 10 + str[idx++] - '0';
  return res;
}

int get_root(int s, int t, string &str) {
  stack<char> st;
  for (int i = s; i <= t; i++) {
    if (st.empty() && isdigit(str[i]))
      return i;
    if (str[i] == '(')
      st.push(str[i]);
    if (str[i] == ')')
      st.pop();
  }
  assert(0);
}

void make_tree(int s, int t, int tree[N], string &str, int idx) {
  while (s < t && check(s, t, str))
    s++, t--;
  if (s >= t)
    return;
  s--, t++;
  int root = get_root(s, t, str);
  if (root == -1)
    return;
  make_tree(s, root - 2, tree, str, idx * 2 + 1);
  tree[idx] = get_num(str, root);
  make_tree(root + 1, t, tree, str, idx * 2 + 2);
}

string make_str(int k) {
  if (AT[k] < 0 || BT[k] < 0)
    return "";
  int num = AT[k] + BT[k];
  string str;
  while (num)
    str = (char)((num % 10) + '0') + str, num /= 10;
  return "(" + make_str(k * 2 + 1) + ")" + "[" + str + "]" + "(" +
         make_str(k * 2 + 2) + ")";
}

int main() {
  string A, B;
  cin >> A >> B;
  memset(AT, -1, sizeof(AT));
  memset(BT, -1, sizeof(BT));
  make_tree(0, A.size() - 1, AT, A, 0);
  make_tree(0, B.size() - 1, BT, B, 0);
  cout << make_str(0) << endl;

  return 0;
}