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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

char ln[7] = {'0', '1', '+', '-', '*', '(', ')'};
int n = 0, ans = -1;
int p[5] = {};
int ind[5] = {};
string s, t;
map<int, int> mp;

string f(string a) {
  if (a == "+" || a == "-" || a == "*" || a == "(" || a == ")")
    return a;
  reverse(a.begin(), a.end());
  int res = 0;
  for (int i = 0; i < a.length(); i++)
    res += (pow(2, i) * (a[i] - '0'));
  ostringstream os;
  os << res;
  return os.str();
}

int cal(vector<string> v) {
  if (v.empty())
    return -1;
  // if(v.size()==3 && (v[1][0]=='0' || v[1][0]=='1') && v[0]=="(" &&
  // v[2]==")")return -1;
  vector<string> vs1, vs2, vs3;
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == "(") {
      vector<string> tmp;
      int l = 1, r = 0, count = 0;
      for (int j = i + 1; j < v.size(); j++) {
        if (v[j] == "(")
          l++;
        if (v[j] == ")")
          r++;
        if ((l - r) == 1 && (v[j] == "+" || v[j] == "*" || v[j] == "-"))
          count++;
        if (r == l) {
          if (count == 0)
            return -1;
          i = j;
          break;
        }
        tmp.push_back(v[j]);
      }
      int res = cal(tmp);
      if (res == -1 || 1024 <= res)
        return -1;
      ostringstream os;
      os << res;
      vs1.push_back(os.str());
    } else
      vs1.push_back(v[i]);
  }
  // if((vs1[1][0]=='0' || vs1[1][0]=='1') && vs1[0]=="(" && vs1[2]==")")return
  // -1;
  if (vs1[0] == "+" || vs1[0] == "*" || vs1[0] == "-")
    return -1;
  int size = vs1.size() - 1;
  if (vs1[size] == "+" || vs1[size] == "*" || vs1[size] == "-")
    return -1;
  for (int i = 0; i < vs1.size(); i++) {
    if (vs1[i] == "*") {
      if ('0' <= vs1[i + 1][0] && vs1[i + 1][0] <= '9' &&
          '0' <= vs2.back()[0] && vs2.back()[0] <= '9') {
        int tmp1 = atoi(vs2.back().c_str()), tmp2 = atoi(vs1[i + 1].c_str());
        if (tmp1 < 0 || 2014 <= tmp1)
          return -1;
        if (tmp2 < 0 || 2014 <= tmp2)
          return -1;
        vs2.pop_back();
        ostringstream os;
        os << (tmp1 * tmp2);
        if (1024 <= tmp1 * tmp2)
          return -1;
        vs2.push_back(os.str());
        i++;
      } else
        return -1;
    } else
      vs2.push_back(vs1[i]);
  }
  for (int i = 0; i < vs2.size(); i++) {
    if (vs2[i] == "+" || vs2[i] == "-") {
      string tmp = vs3.back();
      if ('0' <= vs2[i + 1][0] && vs2[i + 1][0] <= '9' && '0' <= tmp[0] &&
          tmp[0] <= '9') {
        int tmp1 = atoi(tmp.c_str()), tmp2 = atoi(vs2[i + 1].c_str());
        if (tmp1 < 0 || 2014 <= tmp1)
          return -1;
        if (tmp2 < 0 || 2014 <= tmp2)
          return -1;
        vs3.pop_back();
        ostringstream os;
        int res;
        if (vs2[i] == "+")
          res = (tmp1 + tmp2);
        if (vs2[i] == "-")
          res = (tmp1 - tmp2);
        if (res < 0 || 1024 <= res)
          return -1;
        os << res;
        vs3.push_back(os.str());
        i++;
      } else
        return -1;
    } else
      vs3.push_back(vs2[i]);
  }
  int res = -1;
  if (vs3.size() == 1 && '0' <= vs3[0][0] && vs3[0][0] <= '9')
    res = atoi(vs3[0].c_str());
  if (res < 0 || 1024 <= res)
    res = -1;
  return res;
}

void rec(int v) {
  if (v == n) {
    t = s;
    for (int i = 0; i < n; i++)
      t[ind[i]] = ln[p[i]];
    string tmp = "";
    vector<string> vs;
    for (int i = 0; i < t.length(); i++) {
      if (t[i] == '0' || t[i] == '1')
        tmp += t[i];
      else {
        if (tmp != "")
          vs.push_back(tmp);
        tmp = "";
        string st = "";
        st += t[i];
        vs.push_back(st);
      }
    }
    if (tmp != "")
      vs.push_back(tmp);
    stack<int> st;
    for (int i = 0; i < vs.size(); i++) {
      if (vs[i] == "(")
        st.push(i);
      if (vs[i] == ")") {
        if (st.empty())
          return;
        st.pop();
      }
    }
    if (!st.empty())
      return;
    /*
    for(int i=1;i<vs.size()-1;i++){
      if((vs[i][0]=='0' || vs[i][0]=='1') && vs[i-1]=="(" &&
    vs[i+1]==")")return;
    }
    for(int i=1;i<vs.size();i++){
      if(vs[i]==")" && !('0'<=vs[i-1][0] && vs[i-1][0]<='9'))return;
    }
    for(int i=0;i<vs.size()-1;i++){
      if(vs[i]=="(" && !('0'<=vs[i+1][0] && vs[i+1][0]<='9'))return;
      }*/
    for (int i = 0; i < vs.size(); i++)
      vs[i] = f(vs[i]);
    int res = cal(vs);
    ans = max(ans, res);
  } else {
    for (int i = 0; i < 7; i++) {
      rec(v + 1);
      p[v]++;
    }
    p[v] = 0;
  }
}

int main() {
  cin >> s;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '.') {
      ind[n] = i;
      n++;
    }
  }
  rec(0);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

char ln[7] = {'0', '1', '+', '-', '*', '(', ')'};
int n = 0, ans = -1;
int p[5] = {};
int ind[5] = {};
string s, t;
map<int, int> mp;

string f(string a) {
  if (a == "+" || a == "-" || a == "*" || a == "(" || a == ")")
    return a;
  reverse(a.begin(), a.end());
  int res = 0;
  for (int i = 0; i < a.length(); i++)
    res += (pow(2, i) * (a[i] - '0'));
  ostringstream os;
  os << res;
  return os.str();
}

int cal(vector<string> v) {
  if (v.empty())
    return -1;
  vector<string> vs1, vs2, vs3;
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == "(") {
      vector<string> tmp;
      for (int j = i + 1; j < mp[i]; j++)
        tmp.push_back(v[j]);
      int res = cal(tmp);
      if (res == -1 || 1024 <= res)
        return -1;
      ostringstream os;
      os << res;
      vs1.push_back(os.str());
      i = mp[i];
    } else
      vs1.push_back(v[i]);
  }
  if (vs1[0] == "+" || vs1[0] == "*" || vs1[0] == "-")
    return -1;
  int size = vs1.size() - 1;
  if (vs1[size] == "+" || vs1[size] == "*" || vs1[size] == "-")
    return -1;
  for (int i = 0; i < vs1.size(); i++) {
    if (vs1[i] == "*") {
      if ('0' <= vs1[i + 1][0] && vs1[i + 1][0] <= '9' &&
          '0' <= vs2.back()[0] && vs2.back()[0] <= '9') {
        int tmp1 = atoi(vs2.back().c_str()), tmp2 = atoi(vs1[i + 1].c_str());
        vs2.pop_back();
        ostringstream os;
        os << (tmp1 * tmp2);
        if (1024 <= tmp1 * tmp2)
          return -1;
        vs2.push_back(os.str());
        i++;
      } else
        return -1;
    } else
      vs2.push_back(vs1[i]);
  }
  for (int i = 0; i < vs2.size(); i++) {
    if (vs2[i] == "+" || vs2[i] == "-") {
      if ('0' <= vs2[i + 1][0] && vs2[i + 1][0] <= '9' &&
          '0' <= vs3.back()[0] && vs3.back()[0] <= '9') {
        int tmp1 = atoi(vs3.back().c_str()), tmp2 = atoi(vs2[i + 1].c_str());
        vs3.pop_back();
        ostringstream os;
        int res;
        if (vs2[i] == "+")
          res = (tmp1 + tmp2);
        if (vs2[i] == "-")
          res = (tmp1 - tmp2);
        if (res < 0 || 1024 <= res)
          return -1;
        vs3.push_back(os.str());
        i++;
      } else
        return -1;
    } else
      vs3.push_back(vs2[i]);
  }
  if (vs3.size() == 1 && '0' <= vs3[0][0] && vs3[0][0] <= '9')
    return atoi(vs3[0].c_str());
  return -1;
}

void rec(int v) {
  if (v == n) {
    t = s;
    for (int i = 0; i < n; i++)
      t[ind[i]] = ln[p[i]];
    string tmp = "";
    vector<string> vs;
    for (int i = 0; i < t.length(); i++) {
      if (t[i] == '0' || t[i] == '1')
        tmp += t[i];
      else {
        if (tmp != "")
          vs.push_back(tmp);
        tmp = "";
        string st = "";
        st += t[i];
        vs.push_back(st);
      }
    }
    if (tmp != "")
      vs.push_back(tmp);
    stack<int> st;
    mp.clear();
    for (int i = 0; i < vs.size(); i++) {
      if (vs[i] == "(")
        st.push(i);
      if (vs[i] == ")") {
        if (st.empty())
          return;
        mp[st.top()] = i;
        st.pop();
      }
    }
    if (!st.empty())
      return;
    for (int i = 1; i < vs.size() - 1; i++) {
      if ((vs[i][0] == '0' || vs[i][0] == '1') && vs[i - 1] == "(" &&
          vs[i + 1] == ")")
        return;
    }
    for (int i = 0; i < vs.size(); i++)
      vs[i] = f(vs[i]);
    int res = cal(vs);
    ans = max(ans, res);
  } else {
    for (int i = 0; i < 7; i++) {
      rec(v + 1);
      p[v]++;
    }
    p[v] = 0;
  }
}

int main() {
  cin >> s;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '.') {
      ind[n] = i;
      n++;
    }
  }
  rec(0);
  cout << ans << endl;
  return 0;
}