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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

string solve(string s1, string s2) {
  int cnt = 0;
  int l = 1, r = 0;

  int s1n = -1;
  string ls1, rs1;
  for (int i = 1; i < s1.size() - 1; i++) {
    if (s1[i] == '(')
      cnt++;
    else if (s1[i] == ')')
      cnt--;

    if (cnt == 0 && l == 1) {
      r = i;
      ls1 = s1.substr(l, r - l + 1);

      i += 2;

      int l2 = i;
      while (1) {
        i++;
        if (s1[i] == ']') {
          s1n = stoi(s1.substr(l2, i - l2).data());
          i++;
          break;
        }
      }

      l = r = i;
      i--;
    } else if (cnt == 0) {
      r = i;
      rs1 = s1.substr(l, r - l + 1);
    }
  }

  l = 1, r = 0;

  int s2n = -1;
  string ls2, rs2;
  for (int i = 1; i < s2.size() - 1; i++) {
    if (s2[i] == '(')
      cnt++;
    else if (s2[i] == ')')
      cnt--;

    if (cnt == 0 && l == 1) {
      r = i;
      ls2 = s2.substr(l, r - l + 1);

      i += 2;

      int l2 = i;
      while (1) {
        i++;
        if (s2[i] == ']') {
          s2n = stoi(s2.substr(l2, i - l2).data());
          i++;
          break;
        }
      }

      l = r = i;
      i--;
    } else if (cnt == 0) {
      r = i;
      rs2 = s2.substr(l, r - l + 1);
    }
  }

  if (s1n == -1 || s2n == -1)
    return "";

  return "(" + solve(ls1, ls2) + ")" + "[" + to_string(s1n + s2n) + "]" + "(" +
         solve(rs1, rs2) + ")";
}

int main() {
  string s1, s2;
  cin >> s1 >> s2;

  s1.insert(s1.begin(), '(');
  s1.push_back(')');
  s2.insert(s2.begin(), '(');
  s2.push_back(')');

  cout << solve(s1, s2) << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

void solve(string s, long long par, map<long long, int> &m) {
  int cnt = 0;
  int l = 1, r = 0;
  for (int i = 1; i < s.size() - 1; i++) {
    if (s[i] == '(')
      cnt++;
    else if (s[i] == ')')
      cnt--;

    if (cnt == 0) {
      r = i;
      string next = s.substr(l, r - l + 1);

      if (l == 1) {
        solve(next, par * 2 + 1, m);

        i += 2;

        int l2 = i;
        while (1) {
          i++;
          if (s[i] == ']') {
            m[par] = stoi(s.substr(l2, i - l2).data());
            i++;
            break;
          }
        }

        l = r = i;
        i--;
      } else {
        solve(next, par * 2 + 2, m);
      }
    }
  }
}

string solve2(map<long long, int> &m, long long par) {
  string s1, s2, s3;

  if (m.find(par) != m.end()) {
    s2 = to_string(m[par]);
  } else
    return "";

  s1 = solve2(m, par * 2 + 1);
  s3 = solve2(m, par * 2 + 2);

  return "(" + s1 + ")" + "[" + s2 + "]" + "(" + s3 + ")";
}

int main() {
  string s1, s2;
  cin >> s1 >> s2;

  s1.insert(s1.begin(), '(');
  s1.push_back(')');
  s2.insert(s2.begin(), '(');
  s2.push_back(')');

  map<long long, int> a, b;

  solve(s1, 0, a);
  solve(s2, 0, b);

  map<long long, int> c;

  for (auto itr = a.begin(); itr != a.end(); itr++) {
    long long p = itr->first;
    auto itr2 = b.find(p);
    if (itr2 != b.end()) {
      c[p] = itr->second + itr2->second;
    }
  }

  cout << solve2(c, 0) << endl;

  return 0;
}