Problem p01841 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define printV(_v)                                                             \\
  for (auto _x : _v) {                                                         \\
    cout << _x << " ";                                                         \\
  }                                                                            \\
  cout << endl
#define printVS(vs)                                                            \\
  for (auto x : vs) {                                                          \\
    cout << x << endl;                                                         \\
  }
#define printVV(_vv)                                                           \\
  for (auto _v : _vv) {                                                        \\
    for (auto _x : _v) {                                                       \\
      cout << _x << " ";                                                       \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
#define printP(p) cout << p.first << " " << p.second << endl
#define printVP(vp)                                                            \\
  for (auto p : vp)                                                            \\
    printP(p);
#define readV(_v) rep(j, _v.size()) cin >> _v[j];
#define readVV(_vv) rep(i, _vv.size()) readV(_vv[i]);
#define output(_x) cout << _x << endl;

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<Pii> vp;
const int inf = 1e9;
const int mod = 1e9 + 7;

typedef string::const_iterator State;

void assume(State &begin1, State &begin2, char expected, string &s) {
  // cerr << "assume: " << *begin1 << ", " << *begin2 << ", " << expected << ",
  // " << s << endl;

  assert(*begin1 == expected && *begin2 == expected);
  s += *begin1;
  begin1++;
  begin2++;
}

string number(State &begin1, State &begin2) {
  // cerr << "number: " << *begin1 << ", " << *begin2 << endl;

  int val1 = 0;
  while (isdigit(*begin1)) {
    val1 *= 10;
    val1 += *begin1 - '0';
    begin1++;
  }
  int val2 = 0;
  while (isdigit(*begin2)) {
    val2 *= 10;
    val2 += *begin2 - '0';
    begin2++;
  }
  return to_string(val1 + val2);
}

string node(State &begin1, State &begin2) {
  // cerr << "node: " << *begin1 << ", " << *begin2 << endl;

  if (*begin1 == ')') {
    int depth = 1;
    for (;;) {
      if (*begin2 == '(') {
        depth++;
      } else if (*begin2 == ')') {
        depth--;
      }
      if (depth == 0) {
        return "";
      }
      begin2++;
    }
  }
  if (*begin2 == ')') {
    int depth = 1;
    for (;;) {
      if (*begin1 == '(') {
        depth++;
      } else if (*begin1 == ')') {
        depth--;
      }
      if (depth == 0) {
        return "";
      }
      begin1++;
    }
  }

  string ret = "";

  assume(begin1, begin2, '(', ret);
  ret += node(begin1, begin2);
  assume(begin1, begin2, ')', ret);
  assume(begin1, begin2, '[', ret);
  ret += number(begin1, begin2);
  assume(begin1, begin2, ']', ret);
  assume(begin1, begin2, '(', ret);
  ret += node(begin1, begin2);
  assume(begin1, begin2, ')', ret);

  return ret;
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  string s1, s2;
  cin >> s1 >> s2;
  State begin1 = s1.begin();
  State begin2 = s2.begin();
  cout << node(begin1, begin2) << endl;
  assert(begin1 == s1.end() && begin2 == s2.end());
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define printV(_v)                                                             \\
  for (auto _x : _v) {                                                         \\
    cout << _x << " ";                                                         \\
  }                                                                            \\
  cout << endl
#define printVS(vs)                                                            \\
  for (auto x : vs) {                                                          \\
    cout << x << endl;                                                         \\
  }
#define printVV(_vv)                                                           \\
  for (auto _v : _vv) {                                                        \\
    for (auto _x : _v) {                                                       \\
      cout << _x << " ";                                                       \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
#define printP(p) cout << p.first << " " << p.second << endl
#define printVP(vp)                                                            \\
  for (auto p : vp)                                                            \\
    printP(p);
#define readV(_v) rep(j, _v.size()) cin >> _v[j];
#define readVV(_vv) rep(i, _vv.size()) readV(_vv[i]);
#define output(_x) cout << _x << endl;

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<Pii> vp;
const int inf = 1e9;
const int mod = 1e9 + 7;

typedef string::const_iterator State;

void assume(State &begin1, State &begin2, char expected, string &s) {
  // cerr << "assume: " << *begin1 << ", " << *begin2 << ", " << expected << ",
  // " << s << endl;

  assert(*begin1 == expected && *begin2 == expected);
  s += *begin1;
  begin1++;
  begin2++;
}

string number(State &begin1, State &begin2) {
  // cerr << "number: " << *begin1 << ", " << *begin2 << endl;

  int val1 = 0;
  while (isdigit(*begin1)) {
    val1 *= 10;
    val1 += *begin1 - '0';
    begin1++;
  }
  int val2 = 0;
  while (isdigit(*begin2)) {
    val2 *= 10;
    val2 += *begin2 - '0';
    begin2++;
  }
  return to_string(val1 + val2);
}

string node(State &begin1, State &begin2) {
  // cerr << "node: " << *begin1 << ", " << *begin2 << endl;

  if (*begin1 == ')') {
    int depth = 1;
    for (;;) {
      if (*begin2 == '(') {
        depth++;
      } else if (*begin2 == ')') {
        depth--;
      }
      if (depth == 0) {
        return "";
      }
      begin2++;
    }
  }
  if (*begin2 == ')') {
    int depth = 1;
    for (;;) {
      if (*begin1 == '(') {
        depth++;
      } else if (*begin1 == ')') {
        depth--;
      }
      if (depth == 0) {
        return "";
      }
      begin1++;
    }
  }

  string ret = "";

  assume(begin1, begin2, '(', ret);
  ret += node(begin1, begin2);
  assume(begin1, begin2, ')', ret);
  assume(begin1, begin2, '[', ret);
  ret += number(begin1, begin2);
  assume(begin1, begin2, ']', ret);
  assume(begin1, begin2, '(', ret);
  ret += node(begin1, begin2);
  assume(begin1, begin2, ')', ret);

  return ret;
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  string s1, s2;
  while (cin >> s1 >> s2, s1 != "#") {
    State begin1 = s1.begin();
    State begin2 = s2.begin();
    cout << node(begin1, begin2) << endl;
    assert(begin1 == s1.end() && begin2 == s2.end());
  }
}