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

#include <bits/stdc++.h>

using namespace std;
#define int long long
string temp = "01+-*()=";

int A(string &, int &);

int B(string &, int &);

int C(string &, int &);

int C(string &S, int &idx) {
  int ret = 0;
  if (isdigit(S[idx])) {
    if (isdigit(S[idx + 1]) && S[idx] == '0')
      throw(0LL);
    while (isdigit(S[idx]))
      ret = ret * 2 + S[idx++] - '0';
  } else if (S[idx] == '(') {
    ret = A(S, ++idx);
    if (S[idx] != ')')
      throw(0LL);
    ++idx;
  } else if (S[idx] == '-') {
    ret = -C(S, ++idx);
  } else {
    throw(0LL);
  }
  return (ret);
}

int B(string &S, int &idx) {
  int ret = C(S, idx);
  while (S[idx] == '*') {
    ret *= C(S, ++idx);
  }
  return (ret);
}

int A(string &S, int &idx) {
  int ret = B(S, idx);
  while (S[idx] == '+' || S[idx] == '-') {
    if (S[idx] == '+')
      ret += B(S, ++idx);
    else
      ret -= B(S, ++idx);
  }
  return (ret);
}

signed main() {
  string S;
  int conv[32];
  memset(conv, -1, sizeof(conv));

  cin >> S;

  vector<char> num;
  for (char &c : S) {
    if (isalpha(c))
      num.push_back(c);
  }
  sort(begin(num), end(num));
  num.erase(unique(begin(num), end(num)), end(num));
  for (int i = 0; i < S.size(); i++) {
    if (isalpha(S[i]))
      conv[i] = lower_bound(begin(num), end(num), S[i]) - begin(num);
  }

  if (num.size() > temp.size()) {
    cout << 0 << endl;
    return (0);
  }
  sort(begin(temp), end(temp));

  vector<string> ss;
  do {
    string T = S;
    for (int i = 0; i < S.size(); i++) {
      if (~conv[i])
        T[i] = temp[conv[i]];
    }
    if (count(begin(T), end(T), '=') == 1 && T.front() != '=' &&
        T.back() != '=') {
      ss.push_back(T);
    }
  } while (next_permutation(begin(temp), end(temp)));
  sort(begin(ss), end(ss));
  ss.erase(unique(begin(ss), end(ss)), end(ss));

  int ret = 0;
  for (string &al : ss) {
    string L = "", R = "";
    bool which = false;
    for (int i = 0; i < al.size(); i++) {
      if (al[i] == '=') {
        which = true;
        continue;
      }
      (which ? R : L) += al[i];
    }
    R += "$";
    L += "$";
    int idx;

    try {
      int ll = A(L, idx = 0);
      if (idx + 1 != L.size())
        continue;
      int rr = A(R, idx = 0);
      if (idx + 1 != R.size())
        continue;

      ret += ll == rr;
    } catch (int e) {
    }
  }
  cout << ret << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define int long long
string temp = "01+-*()=";

int A(string &, int &);

int B(string &, int &);

int C(string &, int &);

int C(string &S, int &idx) {
  int ret = 0;
  if (isdigit(S[idx])) {
    if (isdigit(S[idx + 1]) && S[idx] == '0')
      throw(0);
    while (isdigit(S[idx]))
      ret = ret * 2 + S[idx++] - '0';
  } else if (S[idx] == '(') {
    ret = A(S, ++idx);
    ++idx;
  } else if (S[idx] == '-') {
    ret = -C(S, ++idx);
  } else {
    throw(0LL);
  }
  return (ret);
}

int B(string &S, int &idx) {
  int ret = C(S, idx);
  while (S[idx] == '*') {
    ret *= C(S, ++idx);
  }
  return (ret);
}

int A(string &S, int &idx) {
  int ret = B(S, idx);
  while (S[idx] == '+' || S[idx] == '-') {
    if (S[idx] == '+')
      ret += A(S, ++idx);
    else
      ret -= A(S, ++idx);
  }
  return (ret);
}

signed main() {
  string S;
  int conv[32];
  memset(conv, -1, sizeof(conv));

  cin >> S;

  vector<char> num;
  for (char &c : S) {
    if (isalpha(c))
      num.push_back(c);
  }
  sort(begin(num), end(num));
  num.erase(unique(begin(num), end(num)), end(num));
  for (int i = 0; i < S.size(); i++) {
    if (isalpha(S[i]))
      conv[i] = lower_bound(begin(num), end(num), S[i]) - begin(num);
  }

  if (num.size() > temp.size()) {
    cout << 0 << endl;
    return (0);
  }
  sort(begin(temp), end(temp));

  vector<string> ss;
  do {
    string T = S;
    for (int i = 0; i < S.size(); i++) {
      if (~conv[i])
        T[i] = temp[conv[i]];
    }
    if (count(begin(T), end(T), '=') == 1 && T.front() != '=' &&
        T.back() != '=') {
      ss.push_back(T);
    }
  } while (next_permutation(begin(temp), end(temp)));
  sort(begin(ss), end(ss));
  ss.erase(unique(begin(ss), end(ss)), end(ss));

  int ret = 0;
  for (string &al : ss) {
    string L = "", R = "";
    bool which = false;
    for (int i = 0; i < al.size(); i++) {
      if (al[i] == '=') {
        which = true;
        continue;
      }
      (which ? R : L) += al[i];
    }
    R += "$";
    L += "$";
    int idx;

    try {
      int ll = A(L, idx = 0);
      if (idx + 1 != L.size())
        continue;
      int rr = A(R, idx = 0);
      if (idx + 1 != R.size())
        continue;

      ret += ll == rr;
    } catch (int e) {
    }
  }
  cout << ret << endl;
}