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

#include <bits/stdc++.h>
using namespace std;
struct no {
  no *left, *right;
  no() { left = right = NULL; }
};
typedef no *node;
string s[2];
int n;
node con(int l, int r) {
  node po = new no();
  int p = 0, c = -1;
  for (int i = l + 1; i < r; i++)
    if (s[n][i] == '(')
      p++;
    else if (s[n][i] == ')')
      p--;
    else if (s[n][i] == ',' && !p) {
      c = i;
      break;
    }
  if (l + 1 != c)
    po->left = con(l + 1, c - 1);
  if (r - 1 != c)
    po->right = con(c + 1, r - 1);
  return po;
}
string init(node a, node b) {
  string l, r;
  if (a->left != NULL && b->left != NULL)
    l = init(a->left, b->left);
  if (a->right != NULL && b->right != NULL)
    r = init(a->right, b->right);
  return "(" + l + "," + r + ")";
}
string unit(node a, node b) {
  string l, r;
  if ((a != NULL && a->left != NULL) || (b != NULL && b->left != NULL))
    l = unit((a != NULL ? a->left : a), (b != NULL ? b->left : b));
  if ((a != NULL && a->right != NULL) || (b != NULL && b->right != NULL))
    r = unit((a != NULL ? a->right : a), (b != NULL ? b->right : b));
  return "(" + l + "," + r + ")";
}
int main() {
  string s1;
  while (cin >> s1 >> s[0] >> s[1]) {
    n = 0;
    node a = con(0, s[0].size() - 1);
    n = 1;
    node b = con(0, s[1].size() - 1);
    if (s1 == "i")
      cout << init(a, b) << endl;
    else
      cout << unit(a, b) << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string o, s, t, ans;
int x, y;
void ii() {
  while (x < s.size() && y < t.size()) {
    if (s[x] == t[y])
      ans += s[x];
    else {
      if (s[x] == '(') {
        stack<char> st;
        st.push(s[x]);
        while (!st.empty()) {
          x++;
          if (s[x] == '.')
            continue;
          else if (s[x] == st.top())
            st.push(s[x]);
          else
            st.pop();
        }
        y--;
        x++;
      } else {
        stack<char> st;
        st.push(t[y]);
        while (!st.empty()) {
          y++;
          if (t[y] == '.')
            continue;
          else if (t[y] == st.top())
            st.push(t[y]);
          else
            st.pop();
        }
        x--;
        y++;
      }
    }
    x++;
    y++;
  }
}
void uu() {
  while (x < s.size() && y < t.size()) {
    if (s[x] == t[y]) {
      ans += s[x];
      x++;
      y++;
    } else if (s[x] == '(') {
      stack<char> st;
      st.push(s[x]);
      ans += s[x];
      while (!st.empty()) {
        x++;
        if (s[x] == ')')
          st.pop(), ans += s[x];
        else if (s[x] == '.')
          st.push(s[x]), ans += s[x];
        else
          st.push('('), ans += s[x];
      }
      y += 2, x++;
    } else {
      stack<char> st;
      st.push(t[y]);
      ans += t[y];
      while (!st.empty()) {
        y++;
        if (t[y] == ')')
          st.pop(), ans += t[y];
        else if (t[y] == '.')
          st.push(t[y]), ans += t[y];
        else
          st.push('('), ans += t[y];
      }
      x += 2, y++;
    }
  }
}
int main() {
  while (cin >> o >> s >> t) {
    ans = "";
    x = y = 0;
    if (s.size() < t.size())
      swap(s, t);
    if (o == "u")
      uu();
    else
      ii();
    cout << ans << endl;
  }
}