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

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

map<char, vector<int>> m;

void compress(vector<int> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}

vector<int> U(vector<int> a, vector<int> b) {
  vector<int> ret;
  set_union(all(a), all(b), back_inserter(ret));
  sort(all(ret));
  return ret;
}

vector<int> I(vector<int> a, vector<int> b) {
  vector<int> ret;
  set_intersection(all(a), all(b), back_inserter(ret));
  sort(all(ret));
  return ret;
}

vector<int> D(vector<int> a, vector<int> b) {
  vector<int> ret;
  set_difference(all(a), all(b), back_inserter(ret));
  sort(all(ret));
  return ret;
}

vector<int> S(vector<int> a, vector<int> b) {
  vector<int> x, y, ret;
  set_difference(all(a), all(b), back_inserter(x));
  set_difference(all(b), all(a), back_inserter(y));
  set_union(all(x), all(y), back_inserter(ret));
  sort(all(ret));
  return ret;
}

vector<int> C(vector<int> a) {
  vector<int> ret;
  set_difference(all(m['U']), all(a), back_inserter(ret));
  sort(all(ret));
  return ret;
}

typedef string::const_iterator State;
vector<int> factor(State &);
vector<int> expression(State &);
void consume(State &, char);

vector<int> factor(State &begin) {
  bool c = false;
  if (*begin == 'c') {
    c = true;
    consume(begin, 'c');
  }

  if (*begin == '(') {
    consume(begin, '(');
    // begin++; // '('????£???°??????
    vector<int> ret = expression(begin);
    consume(begin, ')');
    // begin++; // ')'????£???°??????
    return (c ? C(ret) : ret);
  } else {
    begin++;
    return (c ? C(m[*(begin - 1)]) : m[*(begin - 1)]);
  }
}

// ?????????????????????????????????????????????????????????????????????
vector<int> expression(State &begin) {
  vector<int> ret = factor(begin);

  for (;;) {
    sort(all(ret));
    if (*begin == 'u') {
      consume(begin, 'u');
      ret = U(ret, factor(begin));
    } else if (*begin == 'i') {
      consume(begin, 'i');
      ret = I(ret, factor(begin));
    } else if (*begin == 'd') {
      consume(begin, 'd');
      ret = D(ret, factor(begin));
    } else if (*begin == 's') {
      consume(begin, 's');
      ret = S(ret, factor(begin));
    } else {
      break;
    }
  }

  return ret;
}

// begin???expected?????????????????????begin?????????????????????
void consume(State &begin, char expected) {
  if (*begin == expected) {
    begin++;
  } else {
    cerr << "Expected '" << expected << "' but got '" << *begin << "'" << endl;
    cerr << "Rest string is '";
    while (*begin) {
      cerr << *begin++;
    }
    cerr << "'" << endl;
    assert(0);
    // throw ParseError();
  }
}

int main() {
  while (true) {
    m.clear();
    char c;
    int n;
    bool f = false;
    while (cin >> c >> n) {
      f = true;
      if (c == 'R' && n == 0)
        break;
      int a;
      rep(i, n) {
        cin >> a;
        m['U'].emplace_back(a);
        m[c].emplace_back(a);
      }
    }
    compress(m['U']);
    if (not f)
      break;
    for (auto &it : m)
      sort(all(it.second));

    cin.ignore();
    string s;
    getline(cin, s);
    State tmp = s.begin();
    vector<int> ans = expression(tmp);
    assert(tmp == s.end());
    if (ans.empty())
      cout << "NULL" << endl;
    else
      rep(i, ans.size()) cout << ans[i] << (i == ans.size() - 1 ? '\\n' : ' ');
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

map<char, vector<int>> m;

void compress(vector<int> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}

vector<int> U(vector<int> a, vector<int> b) {
  vector<int> ret;
  set_union(all(a), all(b), back_inserter(ret));
  if (not ret.empty())
    sort(all(ret));
  return ret;
}

vector<int> I(vector<int> a, vector<int> b) {
  vector<int> ret;
  set_intersection(all(a), all(b), back_inserter(ret));
  if (not ret.empty())
    sort(all(ret));
  return ret;
}

vector<int> D(vector<int> a, vector<int> b) {
  vector<int> ret;
  set_difference(all(a), all(b), back_inserter(ret));
  if (not ret.empty())
    sort(all(ret));
  return ret;
}

vector<int> S(vector<int> a, vector<int> b) {
  vector<int> x, y, ret;
  set_difference(all(a), all(b), back_inserter(x));
  set_difference(all(b), all(a), back_inserter(y));
  set_union(all(x), all(y), back_inserter(ret));
  if (not ret.empty())
    sort(all(ret));
  return ret;
}

vector<int> C(vector<int> a) {
  vector<int> ret;
  set_difference(all(m['U']), all(a), back_inserter(ret));
  if (not ret.empty())
    sort(all(ret));
  return ret;
}

typedef string::const_iterator State;
vector<int> factor(State &);
vector<int> expression(State &);
void consume(State &, char);

vector<int> factor(State &begin) {

  if (*begin == '(') {
    consume(begin, '(');
    // begin++; // '('????£???°??????
    vector<int> ret = expression(begin);
    consume(begin, ')');
    // begin++; // ')'????£???°??????
    // return (c ? C(ret) : ret);
    return ret;
  } else {
    bool c = false;
    if (*begin == 'c') {
      c = true;
      begin++;
    }
    begin++;
    return (c ? C(m[*(begin - 1)]) : m[*(begin - 1)]);
  }
}

// ?????????????????????????????????????????????????????????????????????
vector<int> expression(State &begin) {
  vector<int> ret = factor(begin);

  for (;;) {
    sort(all(ret));
    if (*begin == 'u') {
      consume(begin, 'u');
      ret = U(ret, factor(begin));
    } else if (*begin == 'i') {
      consume(begin, 'i');
      ret = I(ret, factor(begin));
    } else if (*begin == 'd') {
      consume(begin, 'd');
      ret = D(ret, factor(begin));
    } else if (*begin == 's') {
      consume(begin, 's');
      ret = S(ret, factor(begin));
    } else {
      break;
    }
  }

  return ret;
}

// begin???expected?????????????????????begin?????????????????????
void consume(State &begin, char expected) {
  if (*begin == expected) {
    begin++;
  } else {
    cerr << "Expected '" << expected << "' but got '" << *begin << "'" << endl;
    cerr << "Rest string is '";
    while (*begin) {
      cerr << *begin++;
    }
    cerr << "'" << endl;
    // throw ParseError();
  }
}

int main() {
  while (true) {
    m.clear();
    char c;
    int n;
    bool f = false;
    while (cin >> c >> n) {
      if (c == 'R' && n == 0)
        break;
      f = true;
      int a;
      rep(i, n) {
        cin >> a;
        m['U'].emplace_back(a);
        m[c].emplace_back(a);
      }
    }
    compress(m['U']);

    if (not f)
      break;

    cin.ignore();
    string s;
    getline(cin, s);
    State tmp = s.begin();
    vector<int> ans = expression(tmp);
    assert(tmp == s.end());
    if (ans.empty())
      cout << '\\0' << endl;
    else
      rep(i, ans.size()) cout << ans[i] << (i == ans.size() - 1 ? '\\n' : ' ');
  }
}