Problem p00736 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
double EPS = 1e-10;
int INF = 1e9;
int MOD = 1000000007;
int _and[3][3] = {0, 0, 0, 0, 1, 1, 0, 1, 2};
int _or[3][3] = {0, 1, 2, 1, 1, 2, 2, 2, 2};
int _not[3] = {2, 1, 0};
char pyon(string s, int p, int q, int r) {
  if (s.size() == 1)
    return s[0] == 'P'   ? p + '0'
           : s[0] == 'Q' ? q + '0'
           : s[0] == 'R' ? r + '0'
                         : s[0];
  int cnt = 0, f, now = -1;
  vector<int> V;
  REP(i, s.size()) {
    if (s[i] == '*')
      f = 0;
    else if (s[i] == '+')
      f = 1;
    else if (s[i] == '-')
      cnt++;
    else {
      int mt = s[i] == 'P' ? p : s[i] == 'Q' ? q : s[i] == 'R' ? r : s[i] - '0';
      if (cnt % 2 == 1) {
        mt = _not[mt];
        cnt = 0;
      }
      if (now == -1)
        now = mt;
      else {
        if (f == 0)
          now = _and[now][mt];
        else
          now = _or[now][mt];
      }
    }
  }
  return now + '0';
}
char rec(string s, int p, int q, int r) {
  stack<int> pr;
  REP(i, s.size()) {
    if (s[i] == '(')
      pr.push(i);
    if (s[i] == ')') {
      int top = pr.top();
      pr.pop();
      string t;
      t.assign(s, top + 1, i - top - 1);
      char tt = pyon(t, p, q, r);
      s.erase(top, i - top + 1);
      s.insert(s.begin() + top, tt);
      i = top - 1;
    }
  }
  char tt = pyon(s, p, q, r);
  return tt;
}
int main() {
  string s;
  while (cin >> s && s != ".") {
    int cnt = 0;
    REP(i, 3) REP(j, 3) REP(k, 3) {
      char t = rec(s, i, j, k);
      if (t == '2')
        cnt++;
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
double EPS = 1e-10;
int INF = 1e9;
int MOD = 1000000007;
int _and[3][3] = {0, 0, 0, 0, 1, 1, 0, 1, 2};
int _or[3][3] = {0, 1, 2, 1, 1, 2, 2, 2, 2};
int _not[3] = {2, 1, 0};
char pyon(string s, int p, int q, int r) {
  int cnt = 0, f;
  vector<int> V;
  REP(i, s.size()) {
    if (s[i] == '*')
      f = 0;
    else if (s[i] == '+')
      f = 1;
    else if (s[i] == '-')
      cnt++;
    else {
      int mt = s[i] == 'P' ? p : s[i] == 'Q' ? q : s[i] == 'R' ? r : s[i] - '0';
      if (cnt % 2 == 1) {
        mt = _not[mt];
        cnt = 0;
      }
      V.PB(mt);
    }
  }
  if (f == 0)
    return (char)(_and[V[0]][V[1]] + '0');
  else
    return (char)(_or[V[0]][V[1]] + '0');
}
int rec(string s, int p, int q, int r) {
  stack<int> pr;
  while (s.size() > 1) {
    REP(i, s.size()) {
      if (s[i] == '(')
        pr.push(i);
      if (s[i] == ')') {
        int top = pr.top();
        pr.pop();
        string t;
        t.assign(s, top + 1, i - top - 1);
        char tt = pyon(t, p, q, r);
        s.erase(top, i - top + 1);
        s.insert(s.begin() + top, tt);
        i = top - 1;
      }
    }
  }
  return (s[0] - '0');
}
int main() {
  string s;
  while (cin >> s && s != ".") {
    int cnt = 0;
    REP(i, 3) REP(j, 3) REP(k, 3) {
      int res = rec(s, i, j, k);
      if (res == 2)
        cnt++;
    }
    cout << cnt << endl;
  }
}