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

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

set<int> Union(const set<int> &A, const set<int> &B) {
  set<int> ret;
  for (set<int>::iterator it = A.begin(); it != A.end(); it++) {
    ret.insert(*it);
  }
  for (set<int>::iterator it = B.begin(); it != B.end(); it++) {
    ret.insert(*it);
  }
  return ret;
}

set<int> Intersection(const set<int> &A, const set<int> &B) {
  set<int> ret;
  for (set<int>::iterator it = B.begin(); it != B.end(); it++) {
    if (A.find(*it) != A.end())
      ret.insert(*it);
  }
  return ret;
}

set<int> Difference(const set<int> &A, const set<int> &B) {
  set<int> ret;
  for (set<int>::iterator it = A.begin(); it != A.end(); it++) {
    if (B.find(*it) == B.end())
      ret.insert(*it);
  }
  return ret;
}

set<int> SymmetricDifference(const set<int> &A, const set<int> &B) {
  return Union(Difference(A, B), Difference(B, A));
}

set<int> Complement(const set<int> &A, const set<int> &U) {
  set<int> ret;
  for (set<int>::iterator it = U.begin(); it != U.end(); it++) {
    if (A.find(*it) == A.end())
      ret.insert(*it);
  }
  return ret;
}

map<char, set<int>> sets;

set<int> analysis(string str) {
  stack<int> s;
  if (str.size() == 1) {
    return sets[str[0]];
  } else if (str[0] == '(' && str[str.size() - 1] == ')') {
    bool f = false;
    for (int i = 0; i < str.size(); i++) {
      if (str[i] == '(') {
        s.push(1);
      } else if (str[i] == ')') {
        s.pop();
      }
      if (i != str.size() - 1 && s.size() == 0)
        f = true;
    }
    if (!f) {
      return analysis(str.substr(1, str.size() - 2));
    }
  }
  // ÊÉðÍ.ãë©ç©Ä¢«Astackª0ÌóÔÅZqð©Â¯½çAÄA·é.
  for (int i = str.size() - 1; i >= 0; i--) {
    if (str[i] == ')') {
      s.push(1);
    } else if (str[i] == '(') {
      s.pop();
    } else if (s.size() == 0) {
      if (str[i] == 'u') {
        return Union(analysis(str.substr(0, i)), analysis(str.substr(i + 1)));
      } else if (str[i] == 'i') {
        return Intersection(analysis(str.substr(0, i)),
                            analysis(str.substr(i + 1)));
      } else if (str[i] == 'd') {
        return Difference(analysis(str.substr(0, i)),
                          analysis(str.substr(i + 1)));
      } else if (str[i] == 's') {
        return SymmetricDifference(analysis(str.substr(0, i)),
                                   analysis(str.substr(i + 1)));
      }
    }
  }
  if (str[0] == 'c') {
    return Complement(analysis(str.substr(1)), sets['U']);
  }
}

int main() {

  while (1) {
    sets.clear();
    for (char ch = 'A'; ch <= 'E'; ch++) {
      sets[ch];
    }
    sets['U'];

    char ch;
    bool f = false;
    while (cin >> ch) {
      if (ch == 'R') {
        int t;
        cin >> t;
        f = true;
        break;
      }
      f = true;
      int n;
      cin >> n;
      for (int i = 0; i < n; i++) {
        int t;
        cin >> t;
        sets[ch].insert(t);
        sets['U'].insert(t);
      }
    }
    if (!f)
      break;
    string str;
    cin >> str;
    set<int> s = analysis(str);
    if (s.size() == 0)
      cout << "NULL" << endl;
    else {
      for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it;
        if (it == --(s.end())) {
          cout << endl;
        } else
          cout << " ";
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

set<int> Union(const set<int> &A, const set<int> &B) {
  set<int> ret;
  for (set<int>::iterator it = A.begin(); it != A.end(); it++) {
    ret.insert(*it);
  }
  for (set<int>::iterator it = B.begin(); it != B.end(); it++) {
    ret.insert(*it);
  }
  return ret;
}

set<int> Intersection(const set<int> &A, const set<int> &B) {
  set<int> ret;
  for (set<int>::iterator it = B.begin(); it != B.end(); it++) {
    if (A.find(*it) != A.end())
      ret.insert(*it);
  }
  return ret;
}

set<int> Difference(const set<int> &A, const set<int> &B) {
  set<int> ret;
  for (set<int>::iterator it = A.begin(); it != A.end(); it++) {
    if (B.find(*it) == B.end())
      ret.insert(*it);
  }
  return ret;
}

set<int> SymmetricDifference(const set<int> &A, const set<int> &B) {
  return Union(Difference(A, B), Difference(B, A));
}

set<int> Complement(const set<int> &A, const set<int> &U) {
  set<int> ret;
  for (set<int>::iterator it = U.begin(); it != U.end(); it++) {
    if (A.find(*it) == A.end())
      ret.insert(*it);
  }
  return ret;
}
map<char, set<int>> sets;

set<int> analysis(string str) {
  stack<int> s;
  if (str.size() == 1) {
    return sets[str[0]];
  } else if (str.size() == 2) {
    return Complement(sets[str[1]], sets['U']);
  } else if (str[0] == '(' && str[str.size() - 1] == ')') {
    bool f = false;
    for (int i = 0; i < str.size(); i++) {
      if (str[i] == '(') {
        s.push(1);
      } else if (str[i] == ')') {
        s.pop();
      }
      if (i != str.size() - 1 && s.size() == 0)
        f = true;
    }
    if (!f) {
      return analysis(str.substr(1, str.size() - 2));
    }
  }
  // ÊÉðÍ.ãë©ç©Ä¢«Astackª0ÌóÔÅZqð©Â¯½çAÄA·é.
  for (int i = str.size() - 1; i >= 0; i--) {
    if (str[i] == ')') {
      s.push(1);
    } else if (str[i] == '(') {
      s.pop();
    } else if (s.size() == 0) {
      if (str[i] == 'u') {
        return Union(analysis(str.substr(0, i)), analysis(str.substr(i + 1)));
      } else if (str[i] == 'i') {
        return Intersection(analysis(str.substr(0, i)),
                            analysis(str.substr(i + 1)));
      } else if (str[i] == 'd') {
        return Difference(analysis(str.substr(0, i)),
                          analysis(str.substr(i + 1)));
      } else if (str[i] == 's') {
        return SymmetricDifference(analysis(str.substr(0, i)),
                                   analysis(str.substr(i + 1)));
      }
    }
  }
}

int main() {

  while (1) {
    sets.clear();
    for (char ch = 'A'; ch <= 'E'; ch++) {
      sets[ch];
    }
    sets['U'];

    char ch;
    bool f = false;
    while (cin >> ch) {
      if (ch == 'R') {
        int t;
        cin >> t;
        f = true;
        break;
      }
      f = true;
      int n;
      cin >> n;
      for (int i = 0; i < n; i++) {
        int t;
        cin >> t;
        sets[ch].insert(t);
        sets['U'].insert(t);
      }
    }
    if (!f)
      break;
    string str;
    cin >> str;
    set<int> s = analysis(str);
    if (s.size() == 0)
      cout << "NULL" << endl;
    else {
      for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it;
        if (it == --(s.end())) {
          cout << endl;
        } else
          cout << " ";
      }
    }
  }

  return 0;
}