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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

void make(vs &v, string s) {
  int n = s.size();
  REP(i, n + 1) {
    REP(j, 10) {
      string t = s;
      if (j == 0 && (i == 0 || !isdigit(s[i - 1])))
        continue;
      if (i > 0 && s[i - 1] == ')')
        continue;
      if (i < n - 1 && s[i] == '(')
        continue;
      string ins;
      ins += '0' + j;
      t.insert(i, ins);
      v.push_back(t);
    }
  }
  FOR(i, 1, n) {
    if (isdigit(s[i - 1]) && isdigit(s[i]) && s[i] != '0') {
      string t = s;
      t.insert(i, "+");
      v.push_back(t);
      t = s;
      t.insert(i, "-");
      v.push_back(t);
      t = s;
      t.insert(i, "*");
      v.push_back(t);
      t = s;
      t.insert(i, "&");
      v.push_back(t);
      t = s;
      t.insert(i, "|");
      v.push_back(t);
      t = s;
      t.insert(i, "^");
      v.push_back(t);
    }
  }
  if (n == 1)
    return;
  REP(i, s.size()) {
    string t = s;
    if (isdigit(s[i])) {
      if (i == 0) {
        if (!isdigit(s[i + 1]) || s[i + 1] == '0')
          continue;
        t.erase(t.begin() + i);
      } else if (i == n - 1) {
        if (!isdigit(s[i - 1]))
          continue;
        t.erase(t.begin() + i);
      } else {
        if (!isdigit(s[i - 1]) && !isdigit(s[i + 1]))
          continue;
        if (s[i + 1] == '0')
          continue;
        t.erase(t.begin() + i);
      }
    } else if (s[i] == '(' || s[i] == ')')
      continue;
    else {
      if (!isdigit(s[i - 1]) || !isdigit(s[i + 1]))
        continue;
      t.erase(t.begin() + i);
    }
    v.push_back(t);
  }
}

int num(const string &s, int &p) {
  assert(isdigit(s[p]));
  int res = 0;
  while (isdigit(s[p])) {
    res *= 10;
    res += s[p] - '0';
    p++;
  }
  return res;
}

int btor(const string &s, int &p);

int term(const string &s, int &p) {
  int res;
  if (s[p] == '(') {
    p++;
    res = btor(s, p);
    assert(s[p] == ')');
    p++;
  } else if (isdigit(s[p])) {
    res = num(s, p);
  } else
    assert(false);
  return res;
}

int mul(const string &s, int &p) {
  int lhs = term(s, p);
  while (s[p] == '*') {
    p++;
    int rhs = term(s, p);
    lhs = lhs * rhs;
  }
  return lhs;
}
int plu(const string &s, int &p) {
  int lhs = mul(s, p);
  while (s[p] == '+' || s[p] == '-') {
    char c = s[p];
    p++;
    int rhs = mul(s, p);
    if (c == '+')
      lhs = lhs + rhs;
    if (c == '-')
      lhs = lhs - rhs;
  }
  return lhs;
}

int btand(const string &s, int &p) {
  int lhs = plu(s, p);
  while (s[p] == '&') {
    p++;
    int rhs = plu(s, p);
    lhs = lhs & rhs;
  }
  return lhs;
}
int btxor(const string &s, int &p) {
  int lhs = btand(s, p);
  while (s[p] == '^') {
    p++;
    int rhs = btand(s, p);
    lhs = lhs ^ rhs;
  }
  return lhs;
}

int btor(const string &s, int &p) {
  int lhs = btxor(s, p);
  while (s[p] == '|') {
    p++;
    int rhs = btxor(s, p);
    lhs = lhs | rhs;
  }
  return lhs;
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n;
  string s, t;
  while (cin >> n >> s, n) {
    vs v;
    make(v, s);
    if (n % 2 == 1) {
      int ans = -INF;
      for (auto i : v) {
        int p = 0;
        chmax(ans, btor(i, p));
      }
      cout << ans << endl;
    } else {
      int ans = -INF;
      for (auto i : v) {
        int tmp = INF;
        vs vv;
        make(vv, i);
        for (auto j : vv) {
          int p = 0;
          chmin(tmp, btor(j, p));
        }
        chmax(ans, tmp);
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

void make(vs &v, string s) {
  int n = s.size();
  REP(i, n + 1) {
    if (i == n || isdigit(s[i])) {
      REP(j, 10) {
        string t = s;
        if (j == 0 && (i == 0 || !isdigit(s[i - 1])))
          continue;
        string ins;
        ins += '0' + j;
        t.insert(i, ins);
        v.push_back(t);
      }
    }
  }
  FOR(i, 1, n) {
    if (isdigit(s[i - 1]) && isdigit(s[i]) && s[i] != '0') {
      string t = s;
      t.insert(i, "+");
      v.push_back(t);
      t = s;
      t.insert(i, "-");
      v.push_back(t);
      t = s;
      t.insert(i, "*");
      v.push_back(t);
      t = s;
      t.insert(i, "&");
      v.push_back(t);
      t = s;
      t.insert(i, "|");
      v.push_back(t);
      t = s;
      t.insert(i, "^");
      v.push_back(t);
    }
  }
  if (n == 1)
    return;
  REP(i, s.size()) {
    string t = s;
    if (isdigit(s[i])) {
      if (i == 0) {
        if (!isdigit(s[i + 1]) || s[i + 1] == '0')
          continue;
        t.erase(t.begin() + i);
      } else if (i == n - 1) {
        if (!isdigit(s[i - 1]))
          continue;
        t.erase(t.begin() + i);
      } else {
        if (!isdigit(s[i - 1]) && !isdigit(s[i + 1]))
          continue;
        if (s[i + 1] == '0')
          continue;
        t.erase(t.begin() + i);
      }
    } else if (s[i] == '(' || s[i] == ')')
      continue;
    else {
      t.erase(t.begin() + i);
    }
    v.push_back(t);
  }
}

int num(const string &s, int &p) {
  assert(isdigit(s[p]));
  int res = 0;
  while (isdigit(s[p])) {
    res *= 10;
    res += s[p] - '0';
    p++;
  }
  return res;
}

int btor(const string &s, int &p);

int term(const string &s, int &p) {
  int res;
  if (s[p] == '(') {
    p++;
    res = btor(s, p);
    assert(s[p] == ')');
    p++;
  } else if (isdigit(s[p])) {
    res = num(s, p);
  } else
    assert(false);
  return res;
}

int mul(const string &s, int &p) {
  int lhs = term(s, p);
  while (s[p] == '*') {
    p++;
    int rhs = term(s, p);
    lhs = lhs * rhs;
  }
  return lhs;
}
int plu(const string &s, int &p) {
  int lhs = mul(s, p);
  while (s[p] == '+' || s[p] == '-') {
    char c = s[p];
    p++;
    int rhs = mul(s, p);
    if (c == '+')
      lhs = lhs + rhs;
    if (c == '-')
      lhs = lhs - rhs;
  }
  return lhs;
}

int btand(const string &s, int &p) {
  int lhs = plu(s, p);
  while (s[p] == '&') {
    p++;
    int rhs = plu(s, p);
    lhs = lhs & rhs;
  }
  return lhs;
}
int btxor(const string &s, int &p) {
  int lhs = btand(s, p);
  while (s[p] == '^') {
    p++;
    int rhs = btand(s, p);
    lhs = lhs ^ rhs;
  }
  return lhs;
}

int btor(const string &s, int &p) {
  int lhs = btxor(s, p);
  while (s[p] == '|') {
    p++;
    int rhs = btxor(s, p);
    lhs = lhs | rhs;
  }
  return lhs;
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n;
  string s, t;
  while (cin >> n >> s, n) {
    vs v;
    make(v, s);
    if (n % 2 == 1) {
      int ans = 0;
      for (auto i : v) {
        int p = 0;
        chmax(ans, btor(i, p));
      }
      cout << ans << endl;
    } else {
      int ans = 0;
      for (auto i : v) {
        int tmp = INF;
        vs vv;
        make(vv, i);
        for (auto j : vv) {
          int p = 0;
          chmin(tmp, btor(j, p));
        }
        chmax(ans, tmp);
      }
      cout << ans << endl;
    }
  }
  return 0;
}