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

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)

int parse(string &s, int &p) {
  if (s[p] == '(') {
    p++;
    int x = parse(s, p);
    char op = s[p++];
    int y = parse(s, p);
    p++;
    if (op == '^') {
      return x ^ y;
    } else {
      return x & y;
    }
  } else if (s[p] == '-') {
    p++;
    int x = parse(s, p);
    return ~x & 65535;
  } else {
    p++;
    if (s[p - 1] == '0')
      return 0x0000;
    if (s[p - 1] == '1')
      return 0xffff;
    if (s[p - 1] == 'a')
      return 0xaaaa;
    if (s[p - 1] == 'b')
      return 0xcccc;
    if (s[p - 1] == 'c')
      return 0xf0f0;
    return 0xff00;
  }
}

int dp[125252];

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
vpi uni;

void dfs2(vpi v) {
  if (v.size() == 1) {
    int m = v[0].first;
    dp[m] = min(dp[m], v[0].second);
    dp[~m & 65535] = min(dp[~m & 65535], v[0].second + 1);
  } else {
    REP(q, 4) {
      REP(i, v.size() - 1) {
        vpi w;
        REP(j, v.size()) if (i == j) {
          pii a = v[j];
          pii b = v[j + 1];
          j++;
          pii c;
          if (q % 2 == 0) {
            c = pii(a.first & b.first, a.second + b.second + 3);
          } else {
            c = pii(a.first ^ b.first, a.second + b.second + 3);
          }
          if (q >= 2) {
            c = pii(~c.first & 65535, c.second + 1);
          }
          w.push_back(c);
        }
        else {
          w.push_back(v[j]);
        }
        dfs2(w);
      }
    }
  }
}

void dfs(int k, vpi v) {
  if (k == 0) {
    dfs2(v);
    return;
  }
  for (pii P : uni) {
    vpi w = v;
    w.push_back(P);
    dfs(k - 1, w);
  }
}

int main() {
  uni.push_back(pii(0x0000, 1));
  uni.push_back(pii(0xffff, 1));
  uni.push_back(pii(0xaaaa, 1));
  uni.push_back(pii(0xcccc, 1));
  uni.push_back(pii(0xf0f0, 1));
  uni.push_back(pii(0xff00, 1));
  uni.push_back(pii(~0xaaaa & 65535, 2));
  uni.push_back(pii(~0xcccc & 65535, 2));
  uni.push_back(pii(~0xf0f0 & 65535, 2));
  uni.push_back(pii(~0xff00 & 65535, 2));

  REP(i, 125252) dp[i] = 83025;

  REP(i, 10) { dp[uni[i].first] = uni[i].second; }

  REP(k, 5) if (k >= 2) {
    dfs(k, vpi(0));
    // cerr << k << endl;
  }

  while (true) {
    string s;
    cin >> s;
    if (s[0] == '.')
      break;
    int p = 0;
    int msk = parse(s, p);
    if (false) {
      int tmp = msk;
      REP(_, 4) {
        cout << (tmp % 16) << " ";
        tmp /= 16;
      }
      cout << endl;
    }
    int ans = s.size();
    ans = min(ans, dp[msk]);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)

int parse(string &s, int &p) {
  if (s[p] == '(') {
    p++;
    int x = parse(s, p);
    char op = s[p++];
    int y = parse(s, p);
    p++;
    if (op == '^') {
      return x ^ y;
    } else {
      return x & y;
    }
  } else if (s[p] == '-') {
    p++;
    int x = parse(s, p);
    return ~x & 65535;
  } else {
    p++;
    if (s[p - 1] == '0')
      return 0x0000;
    if (s[p - 1] == '1')
      return 0xffff;
    if (s[p - 1] == 'a')
      return 0xaaaa;
    if (s[p - 1] == 'b')
      return 0xcccc;
    if (s[p - 1] == 'c')
      return 0xf0f0;
    return 0xff00;
  }
}

int dp[125252];

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
vpi uni;

void dfs2(vpi v) {
  if (v.size() == 1) {
    int m = v[0].first;
    dp[m] = min(dp[m], v[0].second);
    dp[~m & 65535] = min(dp[~m & 65535], v[0].second + 1);
  } else {
    REP(q, 4) {
      REP(i, v.size() - 1) {
        vpi w;
        REP(j, v.size()) if (i == j) {
          pii a = v[j];
          pii b = v[j + 1];
          j++;
          pii c;
          if (q % 2 == 0) {
            c = pii(a.first & b.first, a.second + b.second + 3);
          } else {
            c = pii(a.first ^ b.first, a.second + b.second + 3);
          }
          if (q >= 2) {
            c = pii(~c.first & 65535, c.second + 1);
          }
          w.push_back(c);
        }
        else {
          w.push_back(v[j]);
        }
        dfs2(w);
      }
    }
  }
}

void dfs(int k, vpi v) {
  if (k == 0) {
    dfs2(v);
    return;
  }
  for (pii P : uni) {
    vpi w = v;
    w.push_back(P);
    dfs(k - 1, w);
  }
}

int main() {
  uni.push_back(pii(0x0000, 1));
  uni.push_back(pii(0xffff, 1));
  uni.push_back(pii(0xaaaa, 1));
  uni.push_back(pii(0xcccc, 1));
  uni.push_back(pii(0xf0f0, 1));
  uni.push_back(pii(0xff00, 1));
  uni.push_back(pii(~0xaaaa & 65535, 2));
  uni.push_back(pii(~0xcccc & 65535, 2));
  uni.push_back(pii(~0xf0f0 & 65535, 2));
  uni.push_back(pii(~0xff00 & 65535, 2));

  REP(i, 125252) dp[i] = 83025;

  REP(i, 10) { dp[uni[i].first] = uni[i].second; }

  REP(k, 5) if (k >= 2) {
    dfs(k, vpi(0));
    cerr << k << endl;
  }

  while (true) {
    string s;
    cin >> s;
    if (s[0] == '.')
      break;
    int p = 0;
    int msk = parse(s, p);
    if (false) {
      int tmp = msk;
      REP(_, 4) {
        cout << (tmp % 16) << " ";
        tmp /= 16;
      }
      cout << endl;
    }
    int ans = s.size();
    ans = min(ans, dp[msk]);
    cout << ans << endl;
  }
  return 0;
}