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

#include <bits/stdc++.h>
using namespace std;
#define int long long
int ms = (1 << 16) - 1;
int expr(string s, int &p);
int factor(string s, int &p);
int number(string s, int &p);
int expr(string s, int &p) {
  int res = factor(s, p);
  while (p < (int)s.size()) {
    if (s[p] == '*') {
      p++;
      res &= factor(s, p);
      continue;
    }
    if (s[p] == '^') {
      p++;
      res ^= factor(s, p);
      continue;
    }
    break;
  }
  return res;
}
int factor(string s, int &p) {
  if (s[p] == '-') {
    p++;
    return (~factor(s, p)) & ms;
  }
  if (s[p] == '(') {
    p++;
    int res = expr(s, p);
    assert(s[p] == ')');
    p++;
    return res;
  }
  assert(isalpha(s[p]) || isdigit(s[p]));
  return number(s, p);
}
int calc(char c) {
  if (c == '0')
    return 0b0000000000000000;
  if (c == '1')
    return 0b1111111111111111;
  if (c == 'a')
    return 0b1010101010101010;
  if (c == 'b')
    return 0b1100110011001100;
  if (c == 'c')
    return 0b1111000011110000;
  if (c == 'd')
    return 0b1111111100000000;
  assert(0);
}
int number(string s, int &p) {
  char c = s[p++];
  return calc(c);
}
int dp[1 << 16];
typedef pair<int, int> P;
signed main() {
  for (int i = 0; i < (1 << 16); i++)
    dp[i] = 16;
  priority_queue<P, vector<P>, greater<P>> q;
  string t = "01abcd";
  for (int i = 0; i < (int)t.size(); i++) {
    int k = calc(t[i]);
    dp[k] = 1;
    q.push(P(dp[k], k));
  }
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int k = p.second;
    if (dp[k] < p.first)
      continue;
    // cout<<bitset<16>(k)<<" "<<dp[k]<<endl;
    {
      int nk = (~k) & ms, c = 1;
      if (dp[nk] > dp[k] + c) {
        dp[nk] = dp[k] + c;
        q.push(P(dp[nk], nk));
      }
    }
    for (int i = 0; i < (int)t.size(); i++) {
      int nk = k & calc(t[i]), c = 4;
      if (dp[nk] > dp[k] + c) {
        dp[nk] = dp[k] + c;
        q.push(P(dp[nk], nk));
      }
    }
    for (int i = 0; i < (int)t.size(); i++) {
      int nk = k ^ calc(t[i]), c = 4;
      if (dp[nk] > dp[k] + c) {
        dp[nk] = dp[k] + c;
        q.push(P(dp[nk], nk));
      }
    }
    for (int i = 0; i < (int)t.size(); i++) {
      int nk = k & ((~calc(t[i])) & ms), c = 5;
      if (dp[nk] > dp[k] + c) {
        dp[nk] = dp[k] + c;
        q.push(P(dp[nk], nk));
      }
    }
    for (int i = 0; i < (int)t.size(); i++) {
      int nk = k ^ ((~calc(t[i])) & ms), c = 5;
      if (dp[nk] > dp[k] + c) {
        dp[nk] = dp[k] + c;
        q.push(P(dp[nk], nk));
      }
    }
  }
  string s;
  while (cin >> s, s != ".") {
    int p = 0;
    int k = expr(s, p);
    cout << min((int)s.size(), dp[k]) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int expr(string s, int &p);
int factor(string s, int &p);
int number(string s, int &p);
int expr(string s, int &p) {
  int res = factor(s, p);
  while (p < (int)s.size()) {
    if (s[p] == '*') {
      p++;
      res &= factor(s, p);
      continue;
    }
    if (s[p] == '^') {
      p++;
      res ^= factor(s, p);
      continue;
    }
    break;
  }
  return res;
}
int factor(string s, int &p) {
  if (s[p] == '-') {
    p++;
    return ~factor(s, p);
  }
  if (s[p] == '(') {
    p++;
    int res = expr(s, p);
    assert(s[p] == ')');
    p++;
    return res;
  }
  assert(isalpha(s[p]) || isdigit(s[p]));
  return number(s, p);
}
int calc(char c) {
  if (c == '0')
    return 0b0000000000000000;
  if (c == '1')
    return 0b1111111111111111;
  if (c == 'a')
    return 0b1010101010101010;
  if (c == 'b')
    return 0b1100110011001100;
  if (c == 'c')
    return 0b1111000011110000;
  if (c == 'd')
    return 0b1111111100000000;
  assert(0);
}
int number(string s, int &p) {
  char c = s[p++];
  return calc(c);
}
int dp[1 << 16];
typedef pair<int, int> P;
signed main() {
  int ms = (1 << 16) - 1;
  for (int i = 0; i < (1 << 16); i++)
    dp[i] = 16;
  priority_queue<P, vector<P>, greater<P>> q;
  string t = "01abcd";
  for (int i = 0; i < (int)t.size(); i++) {
    int k = calc(t[i]);
    dp[k] = 1;
    q.push(P(dp[k], k));
  }
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int k = p.second;
    if (dp[k] < p.first)
      continue;
    // cout<<bitset<16>(k)<<" "<<dp[k]<<endl;
    {
      int nk = (~k) & ms, c = 1;
      if (dp[nk] > dp[k] + c) {
        dp[nk] = dp[k] + c;
        q.push(P(dp[nk], nk));
      }
    }
    for (int i = 0; i < (int)t.size(); i++) {
      int nk = k & calc(t[i]), c = 4;
      if (dp[nk] > dp[k] + c) {
        dp[nk] = dp[k] + c;
        q.push(P(dp[nk], nk));
      }
    }
    for (int i = 0; i < (int)t.size(); i++) {
      int nk = k ^ calc(t[i]), c = 4;
      if (dp[nk] > dp[k] + c) {
        dp[nk] = dp[k] + c;
        q.push(P(dp[nk], nk));
      }
    }
  }
  string s;
  while (cin >> s, s != ".") {
    int p = 0;
    int k = expr(s, p);
    cout << min((int)s.size(), dp[k]) << endl;
  }
  return 0;
}