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

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

#define MAX_N 10000

int BIT(int C1, bool C2);
int MINUS(int C3);
int KAKE(int C4, int C5);
int PLUS(int C6, int C7);
string TSTRING(int C8);

string S, T;
int sum;
char U[4] = "012";
int x[MAX_N];

int BIT(int C1, bool C2) {
  if (C2 == true) {
    return C1;
  }
  return MINUS(C1);
}

int MINUS(int C3) { return 2 - C3; }
int PLUS(int C4, int C5) { return max(C4, C5); }
int KAKE(int C6, int C7) { return min(C6, C7); }

string TSTRING(int C8) {
  if (C8 == 0) {
    return "0";
  }
  if (C8 == 1) {
    return "1";
  }
  return "2";
}

int kaiseki2(string Z) {
  int ans = 3, ans2 = 3;
  bool ok = true;
  char op = ' ';
  for (int i = 0; i < Z.size(); i++) {
    if (Z[i] == '-') {
      if (ok == true) {
        ok = false;
      } else {
        ok = true;
      }
    }
    if (ans == 3) {
      if (Z[i] == '0') {
        ans = BIT(0, ok);
        ok = true;
      }
      if (Z[i] == '1') {
        ans = BIT(1, ok);
        ok = true;
      }
      if (Z[i] == '2') {
        ans = BIT(2, ok);
        ok = true;
      }
    } else {
      if (Z[i] == '0') {
        ans2 = BIT(0, ok);
        ok = true;
      }
      if (Z[i] == '1') {
        ans2 = BIT(1, ok);
        ok = true;
      }
      if (Z[i] == '2') {
        ans2 = BIT(2, ok);
        ok = true;
      }
    }
    if (Z[i] == '+') {
      op = '+';
    }
    if (Z[i] == '*') {
      op = '*';
    }
  }
  if (op == ' ') {
    return ans;
  }
  if (op == '+') {
    return PLUS(ans, ans2);
  }
  return KAKE(ans, ans2);
}

int kaiseki(string V) {
  while (V.size() >= 2) {
    int s = 0, maxn = 0;
    string W = "", X = "";
    for (int i = 0; i < V.size(); i++) {
      if (V[i] == '(') {
        s++;
      }
      if (V[i] == ')') {
        s--;
      }
      x[i] = s;
      maxn = max(s, maxn);
    }
    for (int i = 0; i < V.size(); i++) {
      if (x[i] == maxn) {
        if (V[i] != '(') {
          W += V[i];
        }
        goto E;
      }
      if (i >= 1) {
        if (x[i - 1] == maxn) {
          X += TSTRING((long long)kaiseki2(W));
          W = "";
          goto E;
        }
      }
      X += V[i];
    E:;
    }
    V = X;
  }
  if (V == "2") {
    return 1;
  }
  return 0;
}

int main() {
  while (true) {
    cin >> S;
    S = '(' + S + ')';
    sum = 0;
    if (S == "(.)") {
      break;
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          T = "";
          for (int l = 0; l < S.size(); l++) {
            if (S[l] == 'P') {
              T += U[i];
            } else if (S[l] == 'Q') {
              T += U[j];
            } else if (S[l] == 'R') {
              T += U[k];
            } else {
              T += S[l];
            }
          }
          sum += kaiseki(T);
        }
      }
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

#define MAX_N 10000

int BIT(int C1, bool C2);
int MINUS(int C3);
int KAKE(int C4, int C5);
int PLUS(int C6, int C7);
string TSTRING(int C8);

string S, T;
int sum;
char U[4] = "012";
int x[MAX_N];

int BIT(int C1, bool C2) {
  if (C2 == true) {
    return C1;
  }
  return MINUS(C1);
}

int MINUS(int C3) { return 2 - C3; }
int PLUS(int C4, int C5) { return max(C4, C5); }
int KAKE(int C6, int C7) { return max(C6, C7); }

string TSTRING(int C8) {
  if (C8 == 0) {
    return "0";
  }
  if (C8 == 1) {
    return "1";
  }
  return "2";
}

int kaiseki2(string Z) {
  int ans = 3, ans2 = 3;
  bool ok = true;
  char op = ' ';
  for (int i = 0; i < Z.size(); i++) {
    if (Z[i] == '-') {
      if (ok == true) {
        ok = false;
      }
      if (ok == false) {
        ok = true;
      }
    }
    if (ans == 3) {
      if (Z[i] == '0') {
        ans = BIT(0, ok);
        ok = true;
      }
      if (Z[i] == '1') {
        ans = BIT(1, ok);
        ok = true;
      }
      if (Z[i] == '2') {
        ans = BIT(2, ok);
        ok = true;
      }
    } else {
      if (Z[i] == '0') {
        ans2 = BIT(0, ok);
        ok = true;
      }
      if (Z[i] == '1') {
        ans2 = BIT(1, ok);
        ok = true;
      }
      if (Z[i] == '2') {
        ans2 = BIT(2, ok);
        ok = true;
      }
    }
    if (Z[i] == '+') {
      op = '+';
    }
    if (Z[i] == '*') {
      op = '*';
    }
  }
  if (op == ' ') {
    return ans;
  }
  if (op == '+') {
    return PLUS(ans, ans2);
  }
  return KAKE(ans, ans2);
}

int kaiseki(string V) {
  while (V.size() >= 2) {
    int s = 0, maxn = 0;
    string W = "", X = "";
    for (int i = 0; i < V.size(); i++) {
      if (V[i] == '(') {
        s++;
      }
      if (V[i] == ')') {
        s--;
      }
      x[i] = s;
      maxn = max(s, maxn);
    }
    for (int i = 0; i < V.size(); i++) {
      if (x[i] == maxn) {
        if (V[i] != '(') {
          W += V[i];
        }
        goto E;
      }
      if (i >= 1) {
        if (x[i - 1] == maxn) {
          X += TSTRING((long long)kaiseki2(W));
          W = "";
          goto E;
        }
      }
      X += V[i];
    E:;
    }
  }
  if (V == "2") {
    return 1;
  }
  return 0;
}

int main() {
  while (true) {
    cin >> S;
    sum = 0;
    if (S == ".") {
      break;
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          T = "";
          for (int l = 0; l < S.size(); l++) {
            if (S[l] == 'P') {
              T += U[i];
            } else if (S[l] == 'Q') {
              T += U[j];
            } else if (S[l] == 'R') {
              T += U[k];
            } else {
              T += S[l];
            }
          }
          sum += kaiseki(T);
        }
      }
    }
    cout << sum << endl;
  }
}