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

// 18
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<int> v[5], u;
const char *e;
int x;

vector<int> exp();

vector<int> ss() {
  if (e[x] == '(') {
    x++;
    vector<int> r = exp();
    x++;
    return r;
  } else if (e[x] != 'c') {
    return v[e[x++] - 'A'];
  } else {
    x++;
    vector<int> l = ss(), a(u.size());
    vector<int>::iterator it;
    it = set_difference(u.begin(), u.end(), l.begin(), l.end(), a.begin());
    a.resize(it - a.begin());
    return a;
  }
}

vector<int> exp() {
  vector<int> l = ss();
  string op = "uids";
  for (char o = e[x]; op.find(o) != string::npos; o = e[x]) {
    x++;
    vector<int> r = ss(), a(u.size());
    vector<int>::iterator it;
    if (o == 'u') {
      it = set_union(l.begin(), l.end(), r.begin(), r.end(), a.begin());
    } else if (o == 'i') {
      it = set_intersection(l.begin(), l.end(), r.begin(), r.end(), a.begin());
    } else if (o == 'd') {
      it = set_difference(l.begin(), l.end(), r.begin(), r.end(), a.begin());
    } else {
      it = set_symmetric_difference(l.begin(), l.end(), r.begin(), r.end(),
                                    a.begin());
    }
    a.resize(it - a.begin());
    l = a;
  }
  return l;
}

int main() {
  for (;;) {
    u.clear();
    for (int i = 0; i < 5; i++) {
      v[i].clear();
    }
    char c;
    int n;
    while (cin >> c >> n, cin && c != 'R') {
      int x = c - 'A';
      v[x].resize(n);
      while (n--) {
        cin >> v[x][n];
        u.push_back(v[x][n]);
      }
      sort(v[x].begin(), v[x].end());
    }
    if (!cin)
      return 0;
    sort(u.begin(), u.end());
    u.resize(unique(u.begin(), u.end()) - u.begin());
    string s;
    cin >> s;
    e = s.c_str();
    x = 0;
    vector<int> r = exp();
    if (r.empty()) {
      cout << "NULL" << endl;
    } else {
      for (int i = 0; i < r.size(); i++) {
        cout << r[i] << ((i == r.size() - 1) ? '\\n' : ' ');
      }
    }
  }
  return 0;
}

Error: -11

Fixed submission:

// 18
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<int> v[5], u;
const char *e;
int x;

vector<int> exp();

vector<int> ss() {
  if (e[x] == '(') {
    x++;
    vector<int> r = exp();
    x++;
    return r;
  } else if (e[x] != 'c') {
    return v[e[x++] - 'A'];
  } else {
    x++;
    vector<int> l = ss(), a(u.size());
    vector<int>::iterator it;
    it = set_difference(u.begin(), u.end(), l.begin(), l.end(), a.begin());
    a.resize(it - a.begin());
    return a;
  }
}

vector<int> exp() {
  vector<int> l = ss();
  string op = "uids";
  for (char o = e[x]; op.find(o) != string::npos; o = e[x]) {
    x++;
    vector<int> r = ss(), a(u.size());
    vector<int>::iterator it;
    if (o == 'u') {
      it = set_union(l.begin(), l.end(), r.begin(), r.end(), a.begin());
    } else if (o == 'i') {
      it = set_intersection(l.begin(), l.end(), r.begin(), r.end(), a.begin());
    } else if (o == 'd') {
      it = set_difference(l.begin(), l.end(), r.begin(), r.end(), a.begin());
    } else {
      it = set_symmetric_difference(l.begin(), l.end(), r.begin(), r.end(),
                                    a.begin());
    }
    a.resize(it - a.begin());
    l = a;
  }
  return l;
}

int main() {
  for (;;) {
    for (int i = 0; i < 5; i++) {
      v[i].clear();
    }
    char c;
    int n;
    while (cin >> c >> n, c != 'R') {
      if (!cin)
        return 0;
      int x = c - 'A';
      v[x].resize(n);
      while (n--) {
        cin >> v[x][n];
        u.push_back(v[x][n]);
      }
      sort(v[x].begin(), v[x].end());
    }
    sort(u.begin(), u.end());
    u.resize(unique(u.begin(), u.end()) - u.begin());
    string s;
    cin >> s;
    e = s.c_str();
    x = 0;
    vector<int> r = exp();
    if (r.empty()) {
      cout << "NULL" << endl;
    } else {
      for (int i = 0; i < r.size(); i++) {
        cout << r[i] << ((i == r.size() - 1) ? '\\n' : ' ');
      }
    }
  }
  return 0;
}