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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++)
      v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};
int wa[3][3]{{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
int se[3][3]{{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int calc(string &s, int &idx);
int brac(string &s, int &idx);
int number(string &s, int &idx);
int calc(string &s, int &idx) {
  int val1 = brac(s, idx);
  while (s[idx] == '*' || s[idx] == '+') {
    idx++;
    if (s[idx - 1] == '+') {
      int val2 = brac(s, idx);
      val1 = wa[val1][val2];
    } else {
      int val2 = brac(s, idx);
      val1 = se[val1][val2];
    }
  }
  return val1;
}
int brac(string &s, int &idx) {
  int re = 0;
  while (s[idx] == '-') {
    re++;
    idx++;
  }
  if (isdigit(s[idx])) {
    int val = number(s, idx);
    if (re % 2 == 0)
      return val;
    else {
      if (re % 2 == 1)
        if (val == 0)
          val = 2;
        else if (val == 2)
          val = 0;
      return val;
    }
  }
  idx++;
  int val = calc(s, idx);
  idx++;
  if (re % 2 == 1)
    if (val == 0)
      val = 2;
    else if (val == 2)
      val = 0;
  return val;
  return val;
}
int number(string &s, int &idx) {
  int val = s[idx] - '0';
  idx++;

  return val;
}
int main() {
  string s;
  while (cin >> s, s != ".") {
    int ans = 0;
    REP(i, 3) {
      REP(j, 3) {
        REP(k, 3) {
          string s2 = s;
          replace(s2.begin(), s2.end(), 'P', (char)('0' + i));
          replace(s2.begin(), s2.end(), 'Q', (char)('0' + j));
          replace(s2.begin(), s2.end(), 'R', (char)('0' + k));
          int num = 0;
          if (calc(s2, num) == 2)
            ans++;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++)
      v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};
const int wa[3][3]{{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
const int se[3][3]{{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int calc(string &s, int &idx);
int brac(string &s, int &idx);
int number(string &s, int &idx);
int calc(string &s, int &idx) {
  int val1 = brac(s, idx);
  while (s[idx] == '*' || s[idx] == '+') {
    idx++;
    if (s[idx - 1] == '+') {
      int val2 = brac(s, idx);
      val1 = wa[val1][val2];
    } else {
      int val2 = brac(s, idx);
      val1 = se[val1][val2];
    }
  }
  return val1;
}
int brac(string &s, int &idx) {
  if (isdigit(s[idx]) || s[idx] == '-')
    return number(s, idx);
  idx++;
  int val = calc(s, idx);
  idx++;
  return val;
}
int number(string &s, int &idx) {
  int re = 0;
  while (s[idx] == '-') {
    re++;
    idx++;
  }
  int val = s[idx] - '0';
  idx++;
  if (re % 2 == 1)
    if (val == 0)
      val = 2;
    else if (val == 2)
      val = 0;
  return val;
}
int main() {
  string s;
  while (cin >> s, s != ".") {
    int ans = 0;
    REP(i, 3) {
      REP(j, 3) {
        REP(k, 3) {
          string s2 = s;
          replace(s2.begin(), s2.end(), 'P', (char)('0' + i));
          replace(s2.begin(), s2.end(), 'Q', (char)('0' + j));
          replace(s2.begin(), s2.end(), 'R', (char)('0' + k));
          int num = 0;
          if (calc(s2, num) == 2)
            ans++;
        }
      }
    }
    cout << ans << endl;
  }
}