Problem p00736 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int a[] = {2, 1, 0}, b[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}},
    c[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
signed main(void) {
  while (true) {
    string s;
    cin >> s;
    if (s == ".")
      break;
    int ans = 0;
    REP(p, 3) REP(q, 3) REP(r, 3) {
      // cout << p << " " << q << " " << r << endl;
      //???????????°??????????????????
      string t = s;
      REP(i, t.size()) {
        if (t[i] == 'P')
          t[i] = p + '0';
        if (t[i] == 'Q')
          t[i] = q + '0';
        if (t[i] == 'R')
          t[i] = r + '0';
      }
      // cout << t << endl;
      while (true) {
        //???????????????
        int cnt = 0;
        REP(i, t.size()) {
          if (t[i] == '-')
            cnt++;
          else {
            if (cnt > 0) {
              if ('0' <= t[i] && t[i] < '3') {
                int tmp = t[i] - '0';
                // cout << "cnt:" << cnt << endl << tmp << " ";
                REP(j, cnt) {
                  tmp = a[tmp];
                  // if(p == 0 && q == 0 && r == 0) cout << tmp << " ";
                }
                // if(p == 0 && q == 0 && r == 0) cout << endl;
                t = t.substr(0, i - cnt) + (char)(tmp + '0') + t.substr(i + 1);
                i -= cnt;
              }
            }
            cnt = 0;
          }
          // cout << "i:" << i << " cnt:" << cnt << endl;
        }
        // cout << "a:" << t << endl;
        //?????£?????????+??¨*?????????
        REP(i, t.size()) {
          if (t[i] == '+') {
            if (IN('0', '3', t[i - 1]) && IN('0', '3', t[i + 1])) {
              int tmp = c[t[i - 1] - '0'][t[i + 1] - '0'];
              t = t.substr(0, i - 2) + (char)(tmp + '0') + t.substr(i + 3);
              i -= 3;
              // cout << "c:" << t << endl;
            }
          } else if (t[i] == '*') {
            if (IN('0', '3', t[i - 1]) && IN('0', '3', t[i + 1]) &&
                t[i - 2] == '(' && t[i + 2] == ')') {
              int tmp = b[t[i - 1] - '0'][t[i + 1] - '0'];
              t = t.substr(0, i - 2) + (char)(tmp + '0') + t.substr(i + 3);
              i -= 3;
              // cout << "d:" << t << endl;
            }
          }
        }
        // cout << "b:" << t << endl;
        if (t.size() == 1)
          break;
      }
      if (t == "2")
        ans++;
    }
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int a[] = {2, 1, 0}, b[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}},
    c[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
signed main(void) {
  while (true) {
    string s;
    cin >> s;
    if (s == ".")
      break;
    int ans = 0;
    REP(p, 3) REP(q, 3) REP(r, 3) {
      // cout << p << " " << q << " " << r << endl;
      //???????????°??????????????????
      string t = s;
      REP(i, t.size()) {
        if (t[i] == 'P')
          t[i] = p + '0';
        if (t[i] == 'Q')
          t[i] = q + '0';
        if (t[i] == 'R')
          t[i] = r + '0';
      }
      // cout << t << endl;
      while (true) {
        //???????????????
        int cnt = 0;
        REP(i, t.size()) {
          if (t[i] == '-')
            cnt++;
          else {
            if (cnt > 0) {
              if ('0' <= t[i] && t[i] < '3') {
                int tmp = t[i] - '0';
                // cout << "cnt:" << cnt << endl << tmp << " ";
                REP(j, cnt) {
                  tmp = a[tmp];
                  // if(p == 0 && q == 0 && r == 0) cout << tmp << " ";
                }
                // if(p == 0 && q == 0 && r == 0) cout << endl;
                t = t.substr(0, i - cnt) + (char)(tmp + '0') + t.substr(i + 1);
                i -= cnt;
              }
            }
            cnt = 0;
          }
          // cout << "i:" << i << " cnt:" << cnt << endl;
        }
        // cout << "a:" << t << endl;
        //?????£?????????+??¨*?????????
        REP(i, t.size()) {
          if (t[i] == '+') {
            if (IN('0', '3', t[i - 1]) && IN('0', '3', t[i + 1]) &&
                t[i - 2] == '(' && t[i + 2] == ')') {
              int tmp = c[t[i - 1] - '0'][t[i + 1] - '0'];
              t = t.substr(0, i - 2) + (char)(tmp + '0') + t.substr(i + 3);
              i -= 3;
              // cout << "c:" << t << endl;
            }
          } else if (t[i] == '*') {
            if (IN('0', '3', t[i - 1]) && IN('0', '3', t[i + 1]) &&
                t[i - 2] == '(' && t[i + 2] == ')') {
              int tmp = b[t[i - 1] - '0'][t[i + 1] - '0'];
              t = t.substr(0, i - 2) + (char)(tmp + '0') + t.substr(i + 3);
              i -= 3;
              // cout << "d:" << t << endl;
            }
          }
        }
        // cout << "b:" << t << endl;
        if (t.size() == 1)
          break;
      }
      if (t == "2")
        ans++;
    }
    cout << ans << endl;
  }

  return 0;
}