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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int rev[3] = {2, 1, 0};
int seki[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int wa[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};

int f(const string &s, int b, int e) { // [begin,end]
  int ind = -1;
  int nest = 0;
  loop(i, b, e) {
    if (s[i] == '(')
      nest++;
    if (s[i] == ')')
      nest--;
    if ((s[i] == '*' || s[i] == '+') && nest == 0)
      ind = i;
  }
  int ret = -1;
  if (ind < 0) {
    if (s[b] == '(' && s[e] == ')') {
      return f(s, b + 1, e - 1);
    }
    int t = 0;
    while (s[b] == '-')
      b++, t++;
    if (s[b] == '(')
      ret = f(s, b + 1, e);
    else
      ret = s[b] - '0';
    if (t % 2)
      ret = rev[ret];
    // cout << b <<" " << e << " " <<ret <<endl;
    return ret;
  } else {
    ret = f(s, b, ind - 1);
    if (s[ind] == '+')
      ret = wa[ret][f(s, ind + 1, e)];
    else
      ret = seki[ret][f(s, ind + 1, e)];
    //    cout << b << " " << e <<" " <<ret << endl;
    return ret;
  }
}

int solve(string s, int a, int b, int c) {
  int n = s.size();
  rep(i, n) {
    if (s[i] == 'P')
      s[i] = '0' + a;
    if (s[i] == 'Q')
      s[i] = '0' + b;
    if (s[i] == 'R')
      s[i] = '0' + c;
  }
  // cout << s << endl;
  int ret = f(s, 0, n - 1);
  // cout << ret << endl;
  return ret;
}

int main(void) {
  string s;
  while (cin >> s, s != ".") {
    int ans = 0;
    rep(i, 3) rep(j, 3) rep(k, 3) {
      // cout << i << " " << j << " " << k <<endl;
      if (solve(s, i, j, k) == 2)
        ans++;
      //  cout << solve(s,i,j,k) << endl;
    }
    cout << ans << endl;
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int rev[3] = {2, 1, 0};
int seki[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int wa[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};

int f(const string &s, int b, int e) { // [begin,end]
  if (s[b] == '(' && s[e] == ')') {
    return f(s, b + 1, e - 1);
  }
  vi v;
  int ind = -1;
  int nest = 0;
  loop(i, b, e) {
    if (s[i] == '(')
      nest++;
    if (s[i] == ')')
      nest--;
    if ((s[i] == '*' || s[i] == '+') && nest == 0)
      ind = i;
  }
  int ret = -1;
  if (ind < 0) {
    int t = 0;
    loop(i, b, e) if (s[i] == '-') t++;
    if (t % 2) {
      ret = rev[s[e] - '0'];
    } else {
      ret = s[e] - '0';
    }
    // cout << b <<" " << e << " " <<ret <<endl;
    return ret;
  } else {
    ret = f(s, b, ind - 1);
    if (s[ind] == '+')
      ret = wa[ret][f(s, ind + 1, e)];
    else
      ret = seki[ret][f(s, ind + 1, e)];
    //    cout << b << " " << e <<" " <<ret << endl;
    return ret;
  }
}

int solve(string s, int a, int b, int c) {
  int n = s.size();
  rep(i, n) {
    if (s[i] == 'P')
      s[i] = '0' + a;
    if (s[i] == 'Q')
      s[i] = '0' + b;
    if (s[i] == 'R')
      s[i] = '0' + c;
  }
  // cout << s << endl;
  int ret = f(s, 0, n - 1);
  // cout << ret << endl;
  return ret;
}

int main(void) {
  string s;
  while (cin >> s, s != ".") {
    int ans = 0;
    rep(i, 3) rep(j, 3) rep(k, 3) {
      // cout << i << " " << j << " " << k <<endl;
      if (solve(s, i, j, k) == 2)
        ans++;
      //  cout << solve(s,i,j,k) << endl;
    }
    cout << ans << endl;
  }
}