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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X)                                                         \\
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

struct timer {
  time_t start;
  timer() { start = clock(); }
  ~timer() {
    cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
  }
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int, int> pi;
typedef stringstream sst;
typedef vector<int> vi;

int N;
string s, t;
int cur;

int exp6();

int digit() {
  assert(isdigit(s[cur]));
  return s[cur++] - '0';
}

int num() {
  int res = 0;
  while (isdigit(s[cur])) {
    res = res * 10 + digit();
  }
  return res;
}

int exp1() {
  if (isdigit(s[cur])) {
    return num();
  }
  assert(s[cur] == '(');
  cur++;
  int res = exp6();
  assert(s[cur] == ')');
  cur++;
  return res;
}

int exp2() {
  int res = exp1();
  while (s[cur] == '*') {
    cur++;
    res *= exp1();
  }
  return res;
}

int exp3() {
  int res = exp2();
  while (s[cur] == '+' || s[cur] == '-') {
    if (s[cur] == '+') {
      cur++;
      res += exp2();
    } else {
      cur++;
      res -= exp2();
    }
  }
  return res;
}

int exp4() {
  int res = exp3();
  while (s[cur] == '&') {
    cur++;
    res &= exp3();
  }
  return res;
}

int exp5() {
  int res = exp4();
  while (s[cur] == '^') {
    cur++;
    res ^= exp4();
  }
  return res;
}

int exp6() {
  int res = exp5();
  while (s[cur] == '|') {
    cur++;
    res |= exp5();
  }
  return res;
}

string ops = "*+-&^|";

int f(string t, int turn) {
  int res;
  if (turn == 0)
    res = -INF;
  else
    res = INF;
  if (turn == 1)
    ; // cerr<<t<<" -> "<<endl;
  rep(i, sz(t) - 1) {
    if (isdigit(t[i]) || isdigit(t[i + 1])) {
      for (char d = '0'; d <= '9'; d++) {
        if (d == '0' && !isdigit(t[i]))
          continue;
        s = t.substr(0, i + 1) + d + t.substr(i + 1);
        cur = 1;
        if (turn == 0) {
          if (N == 1)
            res = max(res, exp6());
          else
            res = max(res, f(s, 1));
        } else
          res = min(res, exp6());
        if (turn == 1)
          ; // cerr<<s<<endl;
      }
    }
    if (isdigit(t[i]) && isdigit(t[i + 1])) {
      if (t[i + 1] == '0')
        continue;
      rep(j, sz(ops)) {
        s = t.substr(0, i + 1) + ops[j] + t.substr(i + 1);
        cur = 1;
        if (turn == 0) {
          if (N == 1)
            res = max(res, exp6());
          else
            res = max(res, f(s, 1));
        } else
          res = min(res, exp6());
        if (turn == 1)
          ; // cerr<<s<<endl;
      }
    }
  }
  rep2(i, 1, sz(t) - 1) {
    if (isdigit(t[i])) {
      if (!isdigit(t[i - 1]) && !isdigit(t[i + 1]) ||
          !isdigit(t[i - 1]) && t[i + 1] == '0') {
        continue;
      }
      s = t.substr(0, i) + t.substr(i + 1);
      cur = 1;
      if (turn == 0) {
        if (N == 1)
          res = max(res, exp6());
        else
          res = max(res, f(s, 1));
      } else
        res = min(res, exp6());
      if (turn == 1)
        ; // cerr<<s<<endl;
    } else {
      if (!(isdigit(t[i - 1]) && isdigit(t[i + 1]))) {
        continue;
      }
      s = t.substr(0, i) + t.substr(i + 1);
      cur = 1;
      if (turn == 0) {
        if (N == 1)
          res = max(res, exp6());
        else
          res = max(res, f(s, 1));
      } else
        res = min(res, exp6());
      if (turn == 1)
        ; // cerr<<s<<endl;
    }
  }
  if (turn == 1) {
    ; // cerr<<t<<" : "<<res<<endl;
  }
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  while (cin >> N >> t && N) {
    t = " " + t + " ";
    if (N % 2 == 1)
      N = 1;
    else
      N = 2;
    cout << f(t, 0) << endl;
  }
}

Error: 0
 12  -> 
 112 
 212 
 312 
 412 
 512 
 612 
 712 
 812 
 912 
 102 
 112 
 122 
 132 
 142 
 152 
 162 
 172 
 182 
 192 
 1*2 
 1+2 
 1-2 
 1&2 
 1^2 
 1|2 
 120 
 121 
 122 
 123 
 124 
 125 
 126 
 127 
 128 
 129 
 2 
 1 
 12  : -1
 22  -> 
 122 
 222 
 322 
 422 
 522 
 622 
 722 
 822 
 922 
 202 
 212 
 222 
 232 
 242 
 252 
 262 
 272 
 282 
 292 
 2*2 
 2+2 
 2-2 
 2&2 
 2^2 
 2|2 
 220 
 221 
 222 
 223 
 224 
 225 
 226 
 227 
 228 
 229 
 2 
 2 
 22  : 0
 32  -> 
 132 
 232 
 332 
 432 
 532 
 632 
 732 
 832 
 932 
 302 
 312 
 322 
 332 
 342 
 352 
 362 
 372 
 382 
 392 
 3*2 
 3+2 
 3-2 
 3&2 
 3^2 
 3|2 
 320 
 321 
 322 
 323 
 324 
 325 
 326 
 327 
 328 
 329 
 2 
 3 
 32  : 1
 42  -> 
 142 
 242 
 342 
 442 
 542 
 642 
 742 
 842 
 942 
 402 
 412 
 422 
 432 
 442 
 452 
 462 
 472 
 482 
 492 
 4*2 
 4+2 
 4-2 
 4&2 
 4^2 
 4|2 
 420 
 421 
 422 
 423 
 424 
 425 
 426 
 427 
 428 
 429 
 2 
 4 
 42  : 0
 52  -> 
 152 
 252 
 352 
 452 
 552 
 652 
 752 
 852 
 952 
 502 
 512 
 522 
 532 
 542 
 552 
 562 
 572 
 582 
 592 
 5*2 
 5+2 
 5-2 
 5&2 
 5^2 
 5|2 
 520 
 521 
 522 
 523 
 524 
 525 
 526 
 527 
 528 
 529 
 2 
 5 
 52  : 0
 62  -> 
 162 
 262 
 362 
 462 
 562 
 662 
 762 
 862 
 962 
 602 
 612 
 622 
 632 
 642 
 652 
 662 
 672 
 682 
 692 
 6*2 
 6+2 
 6-2 
 6&2 
 6^2 
 6|2 
 620 
 621 
 622 
 623 
 624 
 625 
 626 
 627 
 628 
 629 
 2 
 6 
 62  : 2
 72  -> 
 172 
 272 
 372 
 472 
 572 
 672 
 772 
 872 
 972 
 702 
 712 
 722 
 732 
 742 
 752 
 762 
 772 
 782 
 792 
 7*2 
 7+2 
 7-2 
 7&2 
 7^2 
 7|2 
 720 
 721 
 722 
 723 
 724 
 725 
 726 
 727 
 728 
 729 
 2 
 7 
 72  : 2
 82  -> 
 182 
 282 
 382 
 482 
 582 
 682 
 782 
 882 
 982 
 802 
 812 
 822 
 832 
 842 
 852 
 862 
 872 
 882 
 892 
 8*2 
 8+2 
 8-2 
 8&2 
 8^2 
 8|2 
 820 
 821 
 822 
 823 
 824 
 825 
 826 
 827 
 828 
 829 
 2 
 8 
 82  : 0
 92  -> 
 192 
 292 
 392 
 492 
 592 
 692 
 792 
 892 
 992 
 902 
 912 
 922 
 932 
 942 
 952 
 962 
 972 
 982 
 992 
 9*2 
 9+2 
 9-2 
 9&2 
 9^2 
 9|2 
 920 
 921 
 922 
 923 
 924 
 925 
 926 
 927 
 928 
 929 
 2 
 9 
 92  : 0
 20  -> 
 120 
 220 
 320 
 420 
 520 
 620 
 720 
 820 
 920 
 200 
 210 
 220 
 230 
 240 
 250 
 260 
 270 
 280 
 290 
 200 
 201 
 202 
 203 
 204 
 205 
 206 
 207 
 208 
 209 
 2 
 20  : 2
 21  -> 
 121 
 221 
 321 
 421 
 521 
 621 
 721 
 821 
 921 
 201 
 211 
 221 
 231 
 241 
 251 
 261 
 271 
 281 
 291 
 2*1 
 2+1 
 2-1 
 2&1 
 2^1 
 2|1 
 210 
 211 
 212 
 213 
 214 
 215 
 216 
 217 
 218 
 219 
 1 
 2 
 21  : 0
 22  -> 
 122 
 222 
 322 
 422 
 522 
 622 
 722 
 822 
 922 
 202 
 212 
 222 
 232 
 242 
 252 
 262 
 272 
 282 
 292 
 2*2 
 2+2 
 2-2 
 2&2 
 2^2 
 2|2 
 220 
 221 
 222 
 223 
 224 
 225 
 226 
 227 
 228 
 229 
 2 
 2 
 22  : 0
 23  -> 
 123 
 223 
 323 
 423 
 523 
 623 
 723 
 823 
 923 
 203 
 213 
 223 
 233 
 243 
 253 
 263 
 273 
 283 
 293 
 2*3 
 2+3 
 2-3 
 2&3 
 2^3 
 2|3 
 230 
 231 
 232 
 233 
 234 
 235 
 236 
 237 
 238 
 239 
 3 
 2 
 23  : -1
 24  -> 
 124 
 224 
 324 
 424 
 524 
 624 
 724 
 824 
 924 
 204 
 214 
 224 
 234 
 244 
 254 
 264 
 274 
 284 
 294 
 2*4 
 2+4 
 2-4 
 2&4 
 2^4 
 2|4 
 240 
 241 
 242 
 243 
 244 
 245 
 246 
 247 
 248 
 249 
 4 
 2 
 24  : -2
 25  -> 
 125 
 225 
 325 
 425 
 525 
 625 
 725 
 825 
 925 
 205 
 215 
 225 
 235 
 245 
 255 
 265 
 275 
 285 
 295 
 2*5 
 2+5 
 2-5 
 2&5 
 2^5 
 2|5 
 250 
 251 
 252 
 253 
 254 
 255 
 256 
 257 
 258 
 259 
 5 
 2 
 25  : -3
 26  -> 
 126 
 226 
 326 
 426 
 526 
 626 
 726 
 826 
 926 
 206 
 216 
 226 
 236 
 246 
 256 
 266 
 276 
 286 
 296 
 2*6 
 2+6 
 2-6 
 2&6 
 2^6 
 2|6 
 260 
 261 
 262 
 263 
 264 
 265 
 266 
 267 
 268 
 269 
 6 
 2 
 26  : -4
 27  -> 
 127 
 227 
 327 
 427 
 527 
 627 
 727 
 827 
 927 
 207 
 217 
 227 
 237 
 247 
 257 
 267 
 277 
 287 
 297 
 2*7 
 2+7 
 2-7 
 2&7 
 2^7 
 2|7 
 270 
 271 
 272 
 273 
 274 
 275 
 276 
 277 
 278 
 279 
 7 
 2 
 27  : -5
 28  -> 
 128 
 228 
 328 
 428 
 528 
 628 
 728 
 828 
 928 
 208 
 218 
 228 
 238 
 248 
 258 
 268 
 278 
 288 
 298 
 2*8 
 2+8 
 2-8 
 2&8 
 2^8 
 2|8 
 280 
 281 
 282 
 283 
 284 
 285 
 286 
 287 
 288 
 289 
 8 
 2 
 28  : -6
 29  -> 
 129 
 229 
 329 
 429 
 529 
 629 
 729 
 829 
 929 
 209 
 219 
 229 
 239 
 249 
 259 
 269 
 279 
 289 
 299 
 2*9 
 2+9 
 2-9 
 2&9 
 2^9 
 2|9 
 290 
 291 
 292 
 293 
 294 
 295 
 296 
 297 
 298 
 299 
 9 
 2 
 29  : -7


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X)                                                         \\
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

struct timer {
  time_t start;
  timer() { start = clock(); }
  ~timer() {
    cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
  }
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int, int> pi;
typedef stringstream sst;
typedef vector<int> vi;

int N;
string s, t;
int cur;

int exp6();

int digit() {
  assert(isdigit(s[cur]));
  return s[cur++] - '0';
}

int num() {
  int res = 0;
  while (isdigit(s[cur])) {
    res = res * 10 + digit();
  }
  return res;
}

int exp1() {
  if (isdigit(s[cur])) {
    return num();
  }
  assert(s[cur] == '(');
  cur++;
  int res = exp6();
  assert(s[cur] == ')');
  cur++;
  return res;
}

int exp2() {
  int res = exp1();
  while (s[cur] == '*') {
    cur++;
    res *= exp1();
  }
  return res;
}

int exp3() {
  int res = exp2();
  while (s[cur] == '+' || s[cur] == '-') {
    if (s[cur] == '+') {
      cur++;
      res += exp2();
    } else {
      cur++;
      res -= exp2();
    }
  }
  return res;
}

int exp4() {
  int res = exp3();
  while (s[cur] == '&') {
    cur++;
    res &= exp3();
  }
  return res;
}

int exp5() {
  int res = exp4();
  while (s[cur] == '^') {
    cur++;
    res ^= exp4();
  }
  return res;
}

int exp6() {
  int res = exp5();
  while (s[cur] == '|') {
    cur++;
    res |= exp5();
  }
  return res;
}

string ops = "*+-&^|";

int f(string t, int turn) {
  int res;
  if (turn == 0)
    res = -INF;
  else
    res = INF;
  if (turn == 1)
    cerr << t << " -> " << endl;
  rep(i, sz(t) - 1) {
    if (isdigit(t[i]) || isdigit(t[i + 1])) {
      for (char d = '0'; d <= '9'; d++) {
        if (d == '0' && !isdigit(t[i]))
          continue;
        s = t.substr(0, i + 1) + d + t.substr(i + 1);
        cur = 1;
        if (turn == 0) {
          if (N == 1)
            res = max(res, exp6());
          else
            res = max(res, f(s, 1));
        } else
          res = min(res, exp6());
        if (turn == 1)
          cerr << s << endl;
      }
    }
    if (isdigit(t[i]) && isdigit(t[i + 1])) {
      if (t[i + 1] == '0')
        continue;
      rep(j, sz(ops)) {
        s = t.substr(0, i + 1) + ops[j] + t.substr(i + 1);
        cur = 1;
        if (turn == 0) {
          if (N == 1)
            res = max(res, exp6());
          else
            res = max(res, f(s, 1));
        } else
          res = min(res, exp6());
        if (turn == 1)
          cerr << s << endl;
      }
    }
  }
  rep2(i, 1, sz(t) - 1) {
    if (isdigit(t[i])) {
      if (!isdigit(t[i - 1]) && !isdigit(t[i + 1]) ||
          !isdigit(t[i - 1]) && t[i + 1] == '0') {
        continue;
      }
      s = t.substr(0, i) + t.substr(i + 1);
      cur = 1;
      if (turn == 0) {
        if (N == 1)
          res = max(res, exp6());
        else
          res = max(res, f(s, 1));
      } else
        res = min(res, exp6());
      if (turn == 1)
        cerr << s << endl;
    } else {
      if (!(isdigit(t[i - 1]) && isdigit(t[i + 1]))) {
        continue;
      }
      s = t.substr(0, i) + t.substr(i + 1);
      cur = 1;
      if (turn == 0) {
        if (N == 1)
          res = max(res, exp6());
        else
          res = max(res, f(s, 1));
      } else
        res = min(res, exp6());
      if (turn == 1)
        cerr << s << endl;
    }
  }
  if (turn == 1) {
    cerr << t << " : " << res << endl;
  }
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  while (cin >> N >> t && N) {
    t = " " + t + " ";
    if (N % 2 == 1)
      N = 1;
    else
      N = 2;
    cout << f(t, 0) << endl;
  }
}