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

#include "bits/stdc++.h"
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2740&lang=jp
typedef long long ll;
#define INF 1 << 30
using namespace std;

void solve(int &i1, int &i2, string &S1, string &S2, bool f1 = true,
           bool f2 = true) {
  /* "(" ~ )[x]( ~ ) */
  if (f1 && S1[i1] == '(')
    i1++;
  if (f2 && S2[i2] == '(')
    i2++;
  if (f1 && f2)
    cout << '(';

  /* ( "~" )[x]( ~ ) */
  if (S1[i1] == '(' || S2[i2] == '(') {
    solve(i1, i2, S1, S2, S1[i1] == '(', S2[i2] == '(');
  }

  /* ( ~ ")"[x]( ~ )  */
  if (f1 && S1[i1] == ')')
    i1++;
  if (f2 && S2[i2] == ')')
    i2++;
  if (f1 && f2)
    cout << ')';

  /* ( ~ )"[x]"( ~ ) */
  // "["
  if (f1)
    i1++;
  if (f2)
    i2++;
  // "x"
  int x1 = 0;
  while (f1 && S1[i1] != ']') {
    x1 = x1 * 10 + (S1[i1] - '0');
    i1++;
  }
  int x2 = 0;
  while (f2 && S2[i2] != ']') {
    x2 = x2 * 10 + (S2[i2] - '0');
    i2++;
  }
  // "]"
  if (f1)
    i1++;
  if (f2)
    i2++;

  if (f1 && f2) {
    cout << '[' << x1 + x2 << ']';
  }

  /* ( ~ )[x]"(" ~ ) */
  if (f1 && S1[i1] == '(')
    i1++;
  if (f2 && S2[i2] == '(')
    i2++;
  if (f1 && f2)
    cout << '(';

  /* ( ~ )[x]( "~" )*/
  if (S1[i1] == '(' || S2[i2] == '(') {
    solve(i1, i2, S1, S2, S1[i1] == '(', S2[i2] == '(');
  }

  /* ( ~ )[x]( ~ ")"*/
  if (f1 && S1[i1] == ')')
    i1++;
  if (f2 && S2[i2] == ')')
    i2++;
  if (f1 && f2)
    cout << ')';
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  /* input */
  string A, B;
  cin >> A >> B;

  /* solve */
  int i1 = 0, i2 = 0;
  solve(i1, i2, A, B);
  cout << endl;

  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2740&lang=jp
typedef long long ll;
#define INF 1 << 30
using namespace std;

void check(int &i, int k, vector<int> &str, string &S) {
  /* "(" ~ )[x]( ~ ) */
  if (S[i] == '(')
    i++;

  /* ( "~" )[x]( ~ ) */
  if (S[i] == '(') {
    check(i, 2 * k + 1, str, S);
  }

  /* ( ~ ")"[x]( ~ )  */
  if (S[i] == ')')
    i++;

  /* ( ~ )"[x]"( ~ ) */
  i++; // "["
  str[k] = 0;
  while (S[i] != ']') {
    str[k] = str[k] * 10 + (S[i] - '0');
    i++;
  }
  i++; // "]"

  /* ( ~ )[x]"(" ~ ) */
  if (S[i] == '(')
    i++;

  /* ( ~ )[x]( "~" )*/
  if (S[i] == '(') {
    check(i, 2 * k + 2, str, S);
  }

  /* ( ~ )[x]( ~ ")"*/
  if (S[i] == ')')
    i++;
}

/* print answer method */
void out(int k, vector<int> &Sum) {
  cout << "(";
  if (Sum[2 * k + 1] != -1) {
    out(2 * k + 1, Sum);
  }
  cout << ")[" << Sum[k] << "](";
  if (Sum[2 * k + 2] != -1) {
    out(2 * k + 2, Sum);
  }
  cout << ")";
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  /* input */
  string A, B;
  cin >> A >> B;

  /* initialize */
  int k = max(A.length(), B.length());
  vector<int> A_tree(k * k, -1), B_tree(k * k, -1), Sum_tree(k * k, -1);

  /* calc */
  int x = 0;
  check(x, 0, A_tree, A);
  x = 0;
  check(x, 0, B_tree, B);

  for (int i = 0; i < (int)A_tree.size(); i++) {
    if (A_tree[i] == -1 || B_tree[i] == -1)
      continue;
    Sum_tree[i] = A_tree[i] + B_tree[i];
  }

  /*for (auto v : Sum_tree) {
          cout << v << " ";
  }
  cout << endl;*/

  /* solve */
  out(0, Sum_tree);
  cout << endl;

  return 0;
}