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

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

char rp[] = {'0', '1', '+', '-', '*', '(', ')'};

vector<string> check(vector<string> ss) {
  vector<string> ans;
  for (string x : ss) {
    bool sw = true;
    int c = 0;
    for (int i = 0; i < x.size() && sw; i++) {
      if (x[i] == '(')
        c++;
      if (x[i] == ')')
        c--;
      if (c < 0)
        sw = false;
    }
    if (!c) {
      ans.push_back(x);
    }
  }
  return ans;
}

vector<string> rek(vector<string> ss) {
  vector<string> ans;
  int t;
  if (ss[0].find(".") != string::npos) {
    bool sw = true;
    for (int i = 0; i < ss[0].size() && sw; i++) {
      if (ss[0][i] == '.') {
        t = i;
        sw = false;
      }
    }
    for (string s : ss) {
      for (int i = 0; i < 7; i++) {
        s[t] = rp[i];
        ans.push_back(s);
      }
    }
    ans = rek(ans);
    return ans;
  } else {
    return ss;
  }
}

int ana(string s) {
  int c = 0;
  if (s.size() == 0)
    return -1;
  bool sw = false;
  int res;
  if (s[0] == '(') {
    for (int i = 0; i < s.size() - 1; i++) {
      if (s[i] == '(')
        c++;
      if (s[i] == ')')
        c--;
      if (!c)
        sw = true;
    }
    if (!sw) {
      s = s.substr(1, s.size() - 2);
      c = 0;
      for (int i = 0; i < s.size(); i++) {
        if (s[i] == '(')
          c++;
        if (s[i] == ')')
          c--;
        if (!c && s[i] == '+') {
          int a = ana(s.substr(0, i));
          int b = ana(s.substr(i + 1, s.size() - i - 1));
          if (!(a >= 0 && a < 1024 && b >= 0 && b < 1024))
            return -1;
          res = a + b;
          return (res >= 0 && res < 1024 ? res : -1);
        }
      }
      c = 0;
      for (int i = 0; i < s.size(); i++) {
        if (s[i] == '(')
          c++;
        if (s[i] == ')')
          c--;
        if (!c && s[i] == '-') {
          int a = ana(s.substr(0, i));
          int b = ana(s.substr(i + 1, s.size() - i - 1));
          if (!(a >= 0 && a < 1024 && b >= 0 && b < 1024))
            return -1;
          res = a - b;
          return (res >= 0 && res < 1024 ? res : -1);
        }
      }
      c = 0;
      for (int i = 0; i < s.size(); i++) {
        if (s[i] == '(')
          c++;
        if (s[i] == ')')
          c--;
        if (!c && s[i] == '*') {
          int a = ana(s.substr(0, i));
          int b = ana(s.substr(i + 1, s.size() - i - 1));
          if (!(a >= 0 && a < 1024 && b >= 0 && b < 1024))
            return -1;
          res = a * b;
          return (res >= 0 && res < 1024 ? res : -1);
        }
      }
      return -1;
    }
  }
  c = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '(')
      c++;
    if (s[i] == ')')
      c--;
    if (!c && s[i] == '+') {
      int a = ana(s.substr(0, i));
      int b = ana(s.substr(i + 1, s.size() - i - 1));
      if (!(a >= 0 && a < 1024 && b >= 0 && b < 1024))
        return -1;
      res = a + b;
      return (res >= 0 && res < 1024 ? res : -1);
    }
  }
  c = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '(')
      c++;
    if (s[i] == ')')
      c--;
    if (!c && s[i] == '-') {
      int a = ana(s.substr(0, i));
      int b = ana(s.substr(i + 1, s.size() - i - 1));
      if (!(a >= 0 && a < 1024 && b >= 0 && b < 1024))
        return -1;
      res = a - b;
      return (res >= 0 && res < 1024 ? res : -1);
    }
  }
  c = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '(')
      c++;
    if (s[i] == ')')
      c--;
    if (!c && s[i] == '*') {
      int a = ana(s.substr(0, i));
      int b = ana(s.substr(i + 1, s.size() - i - 1));
      if (!(a >= 0 && a < 1024 && b >= 0 && b < 1024))
        return -1;
      res = a * b;
      return (res >= 0 && res < 1024 ? res : -1);
    }
  }
  if (s.find("(") != string::npos)
    return -1;
  else {
    try {
      res = stoi(s, nullptr, 2);
    } catch (...) {
      return -1;
    }
    return (res >= 0 && res < 1024 ? res : -1);
  }
}

void solve(string s) {
  int ans = -1, anssub = 0;
  vector<string> ss;
  ss.push_back(s);
  ss = rek(ss);
  ss = check(ss);
  for (string x : ss) {
    anssub = ana(x);
    if (anssub < 1024 && anssub >= 0)
      ans = max(ans, anssub);
  }
  cout << ans << endl;
}

int main() {
  string s;
  cin >> s;
  solve(s);
}


Error: 0

Fixed submission:

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

char rp[] = {'0', '1', '+', '-', '*', '(', ')'};

vector<string> check(vector<string> ss) {
  vector<string> ans;
  for (string x : ss) {
    bool sw = true;
    int c = 0;
    for (int i = 0; i < x.size() && sw; i++) {
      if (x[i] == '(')
        c++;
      if (x[i] == ')')
        c--;
      if (c < 0)
        sw = false;
    }
    if (!c) {
      ans.push_back(x);
    }
  }
  return ans;
}

vector<string> rek(vector<string> ss) {
  vector<string> ans;
  int t;
  if (ss[0].find(".") != string::npos) {
    bool sw = true;
    for (int i = 0; i < ss[0].size() && sw; i++) {
      if (ss[0][i] == '.') {
        t = i;
        sw = false;
      }
    }
    for (string s : ss) {
      for (int i = 0; i < 7; i++) {
        s[t] = rp[i];
        ans.push_back(s);
      }
    }
    ans = rek(ans);
    return ans;
  } else {
    return ss;
  }
}

int ana(string s) {
  int c = 0;
  if (s.size() == 0)
    return -1;
  bool sw = false;
  if (s[0] == '(') {
    for (int i = 0; i < s.size() - 1; i++) {
      if (s[i] == '(')
        c++;
      if (s[i] == ')')
        c--;
      if (!c)
        sw = true;
    }
    if (!sw) {
      s = s.substr(1, s.size() - 2);
    }
  }
  int res;
  c = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '(')
      c++;
    if (s[i] == ')')
      c--;
    if (!c && s[i] == '*') {
      int a = ana(s.substr(0, i));
      int b = ana(s.substr(i + 1, s.size() - i - 1));
      if (!(a >= 0 && a < 1024 && b >= 0 && b < 1024))
        return -1;
      res = a * b;
      return (res >= 0 && res < 1024 ? res : -1);
    }
  }
  c = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '(')
      c++;
    if (s[i] == ')')
      c--;
    if (!c && s[i] == '+') {
      int a = ana(s.substr(0, i));
      int b = ana(s.substr(i + 1, s.size() - i - 1));
      if (!(a >= 0 && a < 1024 && b >= 0 && b < 1024))
        return -1;
      res = a + b;
      return (res >= 0 && res < 1024 ? res : -1);
    }
    if (!c && s[i] == '-') {
      int a = ana(s.substr(0, i));
      int b = ana(s.substr(i + 1, s.size() - i - 1));
      if (!(a >= 0 && a < 1024 && b >= 0 && b < 1024))
        return -1;
      res = a - b;
      return (res >= 0 && res < 1024 ? res : -1);
    }
  }
  res = stoi(s, nullptr, 2);
  return (res >= 0 && res < 1024 ? res : -1);
}

void solve(string s) {
  int ans = -1, anssub = 0;
  vector<string> ss;
  ss.push_back(s);
  ss = rek(ss);
  ss = check(ss);
  for (string x : ss) {
    anssub = ana(x);
    if (anssub < 1024 && anssub >= 0)
      ans = max(ans, anssub);
    // cout<<x<<"  "<<anssub<<endl;
  }
  cout << ans << endl;
}

int main() {
  string s;
  cin >> s;
  solve(s);
}