Problem p01624 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int cnt;

char ascnt(const string &s, char c) {
  if (cnt < s.size() && s[cnt] == c) {
    return s[cnt++];
  }
  return 0;
}

int gnum(const string &s) {
  int a = 0;
  if (cnt >= s.size())
    throw 4;
  if ('1' <= s[cnt] && s[cnt] <= '9') {
    while (cnt < s.size() && isdigit(s[cnt])) {
      a *= 10;
      a += ascnt(s, s[cnt]) - '0';
    }
  } else
    throw 4;
  return a;
}

int calc(int a, int b, char c) {
  if (c == '*') {
    return a * b;
  } else if (c == '+') {
    return a + b;
  } else if (c == '-') {
    return a - b;
  } else if (c == '&') {
    return a & b;
  } else if (c == '^') {
    return a ^ b;
  } else if (c == '|') {
    return a | b;
  } else
    throw 3;
}

int ol(const string &s);

int kakko(const string &s) {
  //  cout <<s <<"  " << s[cnt] << " " <<cnt << endl;
  char ka = ascnt(s, '(');
  if (ka) {
    int a = ol(s);
    if (!ascnt(s, ')')) {
      throw -3;
    }
    return a;
  } else {
    return gnum(s);
  }
}

int mult(const string &s) {
  int a = kakko(s), b;
  while (cnt < s.size()) {
    char op = s[cnt];
    if (!ascnt(s, '*'))
      return a;
    b = kakko(s);
    a = calc(a, b, op);
  }
  return a;
}

int pl(const string &s) {
  int a = mult(s), b;
  while (cnt < s.size()) {
    char op = s[cnt];
    if (op == '+')
      ascnt(s, '+');
    else if (op == '-')
      ascnt(s, '-');
    else
      return a;
    b = mult(s);
    a = calc(a, b, op);
  }
  return a;
}

int an(const string &s) {
  int a = pl(s), b;
  while (cnt < s.size()) {
    char op = s[cnt];
    if (!ascnt(s, '&'))
      return a;
    b = pl(s);
    a = calc(a, b, op);
  }
  return a;
}

int xo(const string &s) {
  int a = an(s), b;
  while (cnt < s.size()) {
    char op = s[cnt];
    if (!ascnt(s, '^'))
      return a;
    b = an(s);
    a = calc(a, b, op);
  }
  return a;
}

int ol(const string &s) {
  int a = xo(s), b;
  while (cnt < s.size()) {
    char op = s[cnt];
    if (!ascnt(s, '|'))
      return a;
    b = xo(s);
    a = calc(a, b, op);
  }
  return a;
}

pair<int, bool> isvalid(const string &s) {
  // cout <<s << endl;
  cnt = 0;
  int a;
  try {
    a = ol(s);
  } catch (int e) {
    // cout << e << endl;
    return make_pair(a, false);
  }
  return make_pair(a, s.size() == cnt);
}

string strs = "()*+-&^|0123456789";
vector<string> rekkyo(const string &s) {
  vector<string> ret;
  for (int i = 0; i <= s.size(); i++) {
    string ns;
    for (char c : strs) {
      ns = s.substr(0, i) + c + s.substr(i);
      ret.emplace_back(ns);
    }
    if (i < s.size()) {
      ns = s.substr(0, i) + s.substr(i + 1);
      ret.emplace_back(ns);
    }
  }
  return ret;
}

int solve(int n, int p, string &ns) {
  int ret;
  if (p == 1)
    ret = (1 << 29);
  else
    ret = -(1 << 29);
  for (string ms : rekkyo(ns)) {
    pair<int, bool> rp = isvalid(ms);
    if (!rp.second)
      continue;
    // cout << ms << " " << rp.first << endl;;
    if (n == 1) {
      if (p)
        ret = min(ret, rp.first);
      else
        ret = max(ret, rp.first);
    } else {
      if (p)
        ret = min(ret, solve(n - 1, 1 - p, ms));
      else
        ret = max(ret, solve(n - 1, 1 - p, ms));
    }
  }
  return ret;
}

int main() {
  int N;
  string s;
  while (cin >> N >> s && N) {
    if (N >= 1) {
      if (N & 1)
        N = 1;
      else
        N = 2;
    }
    cout << solve(N, 0, s) << endl;
  }
}

Error: TLE

Fixed submission:

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

int cnt;

char ascnt(const string &s, char c) {
  if (cnt < s.size() && s[cnt] == c) {
    return s[cnt++];
  }
  return 0;
}

int gnum(const string &s) {
  int a = 0;
  if (cnt >= s.size())
    throw 4;
  if ('1' <= s[cnt] && s[cnt] <= '9') {
    while (cnt < s.size() && isdigit(s[cnt])) {
      a *= 10;
      a += ascnt(s, s[cnt]) - '0';
    }
  } else
    throw 4;
  return a;
}

int calc(int a, int b, char c) {
  if (c == '*') {
    return a * b;
  } else if (c == '+') {
    return a + b;
  } else if (c == '-') {
    return a - b;
  } else if (c == '&') {
    return a & b;
  } else if (c == '^') {
    return a ^ b;
  } else if (c == '|') {
    return a | b;
  } else
    throw 3;
}

int ol(const string &s);

int kakko(const string &s) {
  //  cout <<s <<"  " << s[cnt] << " " <<cnt << endl;
  char ka = ascnt(s, '(');
  if (ka) {
    int a = ol(s);
    if (!ascnt(s, ')')) {
      throw -3;
    }
    return a;
  } else {
    return gnum(s);
  }
}

int mult(const string &s) {
  int a = kakko(s), b;
  while (cnt < s.size()) {
    char op = s[cnt];
    if (!ascnt(s, '*'))
      return a;
    b = kakko(s);
    a = calc(a, b, op);
  }
  return a;
}

int pl(const string &s) {
  int a = mult(s), b;
  while (cnt < s.size()) {
    char op = s[cnt];
    if (op == '+')
      ascnt(s, '+');
    else if (op == '-')
      ascnt(s, '-');
    else
      return a;
    b = mult(s);
    a = calc(a, b, op);
  }
  return a;
}

int an(const string &s) {
  int a = pl(s), b;
  while (cnt < s.size()) {
    char op = s[cnt];
    if (!ascnt(s, '&'))
      return a;
    b = pl(s);
    a = calc(a, b, op);
  }
  return a;
}

int xo(const string &s) {
  int a = an(s), b;
  while (cnt < s.size()) {
    char op = s[cnt];
    if (!ascnt(s, '^'))
      return a;
    b = an(s);
    a = calc(a, b, op);
  }
  return a;
}

int ol(const string &s) {
  int a = xo(s), b;
  while (cnt < s.size()) {
    char op = s[cnt];
    if (!ascnt(s, '|'))
      return a;
    b = xo(s);
    a = calc(a, b, op);
  }
  return a;
}

pair<int, bool> isvalid(const string &s) {
  // cout <<s << endl;
  cnt = 0;
  int a;
  try {
    a = ol(s);
  } catch (int e) {
    // cout << e << endl;
    return make_pair(a, false);
  }
  return make_pair(a, s.size() == cnt);
}

string strs = "()*+-&^|0123456789";
vector<string> rekkyo(const string &s) {
  vector<string> ret;
  for (int i = 0; i <= s.size(); i++) {
    string ns;
    for (char c : strs) {
      ns = s.substr(0, i) + c + s.substr(i);
      ret.emplace_back(ns);
    }
    if (i < s.size()) {
      ns = s.substr(0, i) + s.substr(i + 1);
      ret.emplace_back(ns);
    }
  }
  return ret;
}

int solve(int n, int p, string &ns) {
  int ret;
  if (p == 1)
    ret = (1 << 29);
  else
    ret = -(1 << 29);
  for (string ms : rekkyo(ns)) {
    pair<int, bool> rp = isvalid(ms);
    if (!rp.second)
      continue;
    // cout << ms << " " << rp.first << endl;;
    if (n == 1) {
      if (p)
        ret = min(ret, rp.first);
      else
        ret = max(ret, rp.first);
    } else {
      if (p)
        ret = min(ret, solve(n - 1, 1 - p, ms));
      else
        ret = max(ret, solve(n - 1, 1 - p, ms));
    }
  }
  return ret;
}

int main() {
  int N;
  string s;
  while (cin >> N >> s && N) {
    if (N >= 3) {
      if (N & 1)
        N = 3;
      else
        N = 2;
    }
    cout << solve(N, 0, s) << endl;
  }
}