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

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

bool a, b, c, d;

int p;
bool eval(const string &s) {
  if (s[p] == '-') {
    p++;
    return !eval(s);
  } else if (s[p] == '(') {
    p++;
    bool x = eval(s);
    if (s[p] == '^') {
      p++;
      bool y = eval(s);
      assert(s[p] == ')');
      p++;
      return x ^ y;
    } else if (s[p] == '*') {
      p++;
      bool y = eval(s);
      assert(s[p] == ')');
      p++;
      return x & y;
    } else
      assert(false);
  }
  switch (s[p++]) {
  case '0':
    return false;
  case '1':
    return true;
  case 'a':
    return a;
  case 'b':
    return b;
  case 'c':
    return c;
  case 'd':
    return d;
  default:
    dbg(s, p);
  }
  assert(false);
}

int doall(const string &s) {
  int x = 0;
  rep(i, 2) rep(j, 2) rep(k, 2) rep(l, 2) {
    a = i;
    b = j;
    c = k;
    d = l;
    p = 0;
    x = x * 2 + eval(s);
  }
  return x;
}

int main() {
  unordered_set<string> s;
  s.insert("a");
  s.insert("b");
  s.insert("c");
  s.insert("d");
  s.insert("-a");
  s.insert("-b");
  s.insert("-c");
  s.insert("-d");
  rep(i, 3) {
    unordered_set<string> ns(s);
    for (auto &ps : s)
      for (auto &pt : s)
        if (ps.size() + pt.size() <= 13 && ps < pt) {
          string s1 = "(" + ps + "^" + pt + ")";
          string s2 = "(" + ps + "*" + pt + ")";
          ns.insert(s1);
          ns.insert(s2);
          if (s1.size() < 16) {
            ns.insert("-" + s1);
            ns.insert("-" + s2);
          }
        }
    swap(ns, s);
    // dbg(vector<string>(all(s)),i,s.size());
  }
  s.insert("0");
  s.insert("1");

  vector<int> vec(1 << 16, INF);
  for (auto &str : s) {
    int v = doall(str);
    vec[v] = min<int>(vec[v], str.size());
  }

  string str;
  while (cin >> str, str != ".") {
    int v = doall(str);
    cout << vec[v] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

bool a, b, c, d;

int p;
bool eval(const string &s) {
  if (s[p] == '-') {
    p++;
    return !eval(s);
  } else if (s[p] == '(') {
    p++;
    bool x = eval(s);
    if (s[p] == '^') {
      p++;
      bool y = eval(s);
      assert(s[p] == ')');
      p++;
      return x ^ y;
    } else if (s[p] == '*') {
      p++;
      bool y = eval(s);
      assert(s[p] == ')');
      p++;
      return x & y;
    } else
      assert(false);
  }
  switch (s[p++]) {
  case '0':
    return false;
  case '1':
    return true;
  case 'a':
    return a;
  case 'b':
    return b;
  case 'c':
    return c;
  case 'd':
    return d;
  default:
    dbg(s, p);
  }
  assert(false);
}

int doall(const string &s) {
  int x = 0;
  rep(i, 2) rep(j, 2) rep(k, 2) rep(l, 2) {
    a = i;
    b = j;
    c = k;
    d = l;
    p = 0;
    x = x * 2 + eval(s);
  }
  return x;
}

int main() {
  unordered_set<string> s;
  s.insert("a");
  s.insert("b");
  s.insert("c");
  s.insert("d");
  s.insert("-a");
  s.insert("-b");
  s.insert("-c");
  s.insert("-d");
  rep(_, 3) {
    unordered_set<string> ns(s);
    for (auto &ps : s)
      for (auto &pt : s)
        if (ps.size() + pt.size() <= 13 && ps < pt) {
          string s1 = "(" + ps + "^" + pt + ")";
          string s2 = "(" + ps + "*" + pt + ")";
          ns.insert(s1);
          ns.insert(s2);
          if (s1.size() < 16) {
            ns.insert("-" + s1);
            ns.insert("-" + s2);
          }
        }
    swap(ns, s);
    // dbg(vector<string>(all(s)),_,s.size());
  }
  s.insert("0");
  s.insert("1");

  vector<int> vec(1 << 8, INF);
  vector<string> vvv(all(s));
  for (auto &str : vvv) {
    int v = doall(str);
    vec[v] = min<int>(vec[v], str.size());
  }

  string str;
  while (cin >> str, str != ".") {
    int v = doall(str);
    cout << vec[v] << endl;
  }
  exit(0);

  return 0;
}