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

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

const int MAXN = 501;

char A[5][MAXN];
int pos;
string line;

vector<char> exp();
vector<char> fact();

vector<char> exp() {
  vector<char> res = fact();
  while (1) {
    if (line[pos] == 'u') {
      ++pos;
      vector<char> x = fact();
      for (int i = 0; i < x.size(); ++i) {
        res[i] = (res[i] || x[i]);
      }
    } else if (line[pos] == 'i') {
      ++pos;
      vector<char> x = fact();
      for (int i = 0; i < x.size(); ++i) {
        res[i] = (res[i] && x[i]);
      }
    } else if (line[pos] == 'd') {
      ++pos;
      vector<char> x = fact();
      for (int i = 0; i < x.size(); ++i) {
        res[i] = (res[i] && !x[i]);
      }
    } else if (line[pos] == 's') {
      ++pos;
      vector<char> x = fact();
      for (int i = 0; i < x.size(); ++i) {
        res[i] = ((res[i] && !x[i]) || (x[i] && !res[i]));
      }
    } else {
      break;
    }
  }
  return res;
}

vector<char> fact() {
  vector<char> res;
  if (line[pos] == 'c') {
    ++pos;
    res = fact();
    for (int i = 0; i < res.size(); ++i) {
      res[i] = 1 - res[i];
    }
  } else if (isupper(line[pos])) {
    char c = line[pos];
    ++pos;
    res = vector<char>(A[c - 'A'], A[c - 'A'] + MAXN);
  } else if (line[pos] == '(') {
    ++pos;
    res = exp();
    ++pos;
  }
  return res;
}

map<int, int> id, rid;
int getId(int n) {
  if (!id.count(n)) {
    id[n] = id.size() - 1;
    rid[id[n]] = n;
  }
  return id[n];
}

int main() {
  while (1) {
    bool run = true;
    fill(A[0], A[5], false);
    id.clear();
    rid.clear();
    while (1) {
      char c;
      int n;
      if (!(cin >> c >> n)) {
        run = false;
        break;
      }
      if (c == 'R' && n == 0)
        break;
      for (int i = 0; i < n; ++i) {
        int a;
        cin >> a;
        a = getId(a);
        A[c - 'A'][a] = true;
      }
    }
    if (!run)
      break;
    string s;
    cin >> s;
    line = s;
    pos = 0;
    vector<char> res = exp();
    vector<int> ans;
    for (int i = 0, cnt = 0; i < res.size(); ++i) {
      if (rid.count(i) && res[i]) {
        ans.push_back(rid[i]);
      }
    }
    sort(ans.begin(), ans.end());
    if (ans.size() == 0) {
      cout << "NULL" << endl;
    } else {
      for (int i = 0; i < ans.size(); ++i) {
        if (i)
          cout << " ";
        cout << ans[i];
      }
      cout << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

const int MAXN = 501;

char A[5][MAXN];
int pos;
string line;

vector<char> exp();
vector<char> fact();

vector<char> exp() {
  vector<char> res = fact();
  while (1) {
    if (line[pos] == 'u') {
      ++pos;
      vector<char> x = fact();
      for (int i = 0; i < x.size(); ++i) {
        res[i] = (res[i] || x[i]);
      }
    } else if (line[pos] == 'i') {
      ++pos;
      vector<char> x = fact();
      for (int i = 0; i < x.size(); ++i) {
        res[i] = (res[i] && x[i]);
      }
    } else if (line[pos] == 'd') {
      ++pos;
      vector<char> x = fact();
      for (int i = 0; i < x.size(); ++i) {
        res[i] = (res[i] && !x[i]);
      }
    } else if (line[pos] == 's') {
      ++pos;
      vector<char> x = fact();
      vector<char> a(res.size(), 0), b(res.size(), 0);
      for (int i = 0; i < x.size(); ++i) {
        a[i] = (res[i] && !x[i]);
      }
      for (int i = 0; i < x.size(); ++i) {
        b[i] = (x[i] && !res[i]);
      }
      for (int i = 0; i < x.size(); ++i) {
        res[i] = (a[i] || b[i]);
      }
    } else {
      break;
    }
  }
  return res;
}

vector<char> fact() {
  vector<char> res;
  if (line[pos] == 'c') {
    ++pos;
    res = fact();
    for (int i = 0; i < res.size(); ++i) {
      res[i] = 1 - res[i];
    }
  } else if (isupper(line[pos])) {
    char c = line[pos];
    ++pos;
    res = vector<char>(A[c - 'A'], A[c - 'A'] + MAXN);
  } else if (line[pos] == '(') {
    ++pos;
    res = exp();
    ++pos;
  }
  return res;
}

int main() {
  while (1) {
    bool run = true;
    set<int> num;
    fill(A[0], A[5], false);
    while (1) {
      char c;
      int n;
      if (!(cin >> c >> n)) {
        run = false;
        break;
      }
      if (c == 'R' && n == 0)
        break;
      for (int i = 0; i < n; ++i) {
        int a;
        cin >> a;
        num.insert(a);
        A[c - 'A'][a] = true;
      }
    }
    if (!run)
      break;
    string s;
    cin >> s;
    line = s;
    pos = 0;
    vector<char> res = exp();
    for (int i = 0, cnt = 0; i < res.size(); ++i) {
      if (num.count(i) && res[i]) {
        if (cnt++)
          cout << " ";
        cout << i;
      }
    }
    cout << endl;
  }
  return 0;
}