Problem:
Problem C: How can I satisfy thee? Let me count the ways...

Three-valued logic is a logic system that has, in addition to "true" and
"false", "unknown" as a valid value.

In the following, logical values "false", "unknown" and "true" are
represented by 0, 1 and 2 respectively.

Let "-" be a unary operator (i.e. a symbol representing one argument function)
and let both "*" and "+" be binary operators (i.e. symbols representing
two argument functions).
These operators represent negation (NOT), conjunction (AND) and
disjunction (OR) respectively.
These operators in three-valued logic can be defined in Table C-1.

Table C-1: Truth tables of three-valued logic operators

-X

(X*Y)

(X+Y)

Let P, Q and R be variables ranging over three-valued logic values.
For a given formula, you are asked to answer the number of triples (P,Q,R)
that satisfy the formula, that is, those which make the value of the
given formula 2.
A formula is one of the following form (X and Y represent formulas).

Constants: 0, 1 or 2

Variables: P, Q or R

Negations: -X

Conjunctions: (X*Y)

Disjunctions: (X+Y)

Note that conjunctions and disjunctions of two formulas are always
parenthesized.

For example, when formula (P*Q) is given as an input, the value of
this formula is 2 when and only when (P,Q,R) is (2,2,0), (2,2,1) or (2,2,2).
Therefore, you should output 3.

Input

The input consists of one or more lines. Each line contains a formula.
A formula is a string which consists of 0, 1, 2, P, Q, R, -, *, +, (, ).
Other characters such as spaces are not contained.
The grammar of formulas is given by the following BNF.

<formula> ::= 0 | 1 | 2 | P | Q | R |
-<formula> | (<formula>*<formula>) | (<formula>+<formula>)

All the formulas obey this syntax and thus you do not have to care about
grammatical errors.
Input lines never exceed 80 characters.

Finally, a line which
contains only a "." (period) comes, indicating the end of the input.

Output

You should answer the number (in decimal) of triples (P,Q,R) that make the
value of the given formula 2. One line containing the number should be
output for each of the formulas, and no other characters should be output.

Sample Input

(P*Q)
(--R+(P*Q))
(P*-P)
2
1
(-1+(((---P+Q)*(--Q+---R))*(-R+-P)))
.

Output for the Sample Input

3
11
0
27
0
7

Solution in C++ (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)

int bar[3] = { 2, 1, 0 };

int mul[3][3] = {
	{ 0, 0, 0 },
	{ 0, 1, 1 },
	{ 0, 1, 2 }
};

int add[3][3] = {
	{ 0, 1, 2 },
	{ 1, 1, 2 },
	{ 2, 2, 2 }
};

string str;
int var[3];

int parse(int l, int r) {
	int arc = 0, nl = -1;
	stack< char > op;
	stack< int > val;
	
	for_(i,l,r) {
		if (arc == 0 && str[i] == '(') {
			++arc;
			nl = i + 1;
		} else if (arc > 0) {
			if (str[i] == ')') {
				--arc;
				if (arc == 0) val.push(parse(nl, i));
			} else if (str[i] == '(') {
				++arc;
			}
		} else if (arc == 0) {
			if (isdigit(str[i])) val.push(str[i] - '0');
			else if (str[i] == 'P') val.push(var[0]);
			else if (str[i] == 'Q') val.push(var[1]);
			else if (str[i] == 'R') val.push(var[2]);
			else op.push(str[i]);
		}
	}
	
	if (val.size() == 1) {
		int res = val.top(); val.pop();
		
		while (!op.empty() && op.top() == '-') {
			op.pop();
			res = bar[res];
		}
		
		return res;
	}
	
	int x2 = val.top(); val.pop();
	while (op.top() == '-') {
		op.pop();
		x2 = bar[x2];
	}
	
	char opp = op.top(); op.pop();
	
	int x1 = val.top(); val.pop();
	while (!op.empty() && op.top() == '-') {
		op.pop();
		x1 = bar[x1];
	}
	
	return (opp == '*' ? mul[x1][x2] : add[x1][x2]);
}

int main() {
	while (cin >> str) {
		if (str == ".") break;
		
		int ans = 0;
		
		for_(p,0,3) for_(q,0,3) for_(r,0,3) {
			var[0] = p; var[1] = q; var[2] = r;
			if (parse(0, (int)str.size()) == 2) ++ans;
		}
		
		cout << ans << endl;
	}
}