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 JAVA (judge verdict: Wrong Answer)

import java.util.Scanner;

public class Main {
	static int and[][] = { { 0, 0, 0 }, { 0, 1, 1 }, { 0, 1, 2 } };// i*j
	static int or[][] = { { 0, 1, 2 }, { 1, 1, 2 }, { 2, 2, 2 } };

	static boolean isNum(char c) {
		return '0' <= c && c <= '9';
	}

	static void replace(StringBuilder sb, int p, int q, int r) {
		for (int i = 0; i < sb.length(); i++) {
			if (sb.charAt(i) == 'P') {
				sb.setCharAt(i, (char) (p + '0'));
			} else if (sb.charAt(i) == 'Q') {
				sb.setCharAt(i, (char) (q + '0'));
			} else if (sb.charAt(i) == 'R') {
				sb.setCharAt(i, (char) (r + '0'));
			}
		}
	}

	static void negRemove(StringBuilder sb) {
		int past = sb.length() + 10, now;
		while (true) {
			now = sb.length();
			if (past == now) {
				return;
			}
			for (int i = 0; i + 1 < sb.length(); i++) {
				if (sb.charAt(i) == '-' && sb.charAt(i + 1) == '-') {
					sb.delete(i, i + 2);
				}
			}
			past = now;
		}
	}

	static void negate(StringBuilder sb) {
		for (int i = 0; i + 1 < sb.length(); i++) {
			if (sb.charAt(i) == '-' && isNum(sb.charAt(i + 1))) {
				// -0 -> 2
				// -1 -> 1
				// -2 -> 0
				char c = sb.charAt(i + 1);
				if (c == '0') {
					sb.setCharAt(i + 1, '2');
					sb.delete(i, i + 1);
				} else if (c == '1') {
					sb.delete(i, i + 1);
				} else {
					sb.setCharAt(i + 1, '0');
					sb.delete(i, i + 1);
				}
			}
		}
	}

	static void calc(StringBuilder sb) {
		for (int i = 0; i + 2 < sb.length(); i++) {
			char l, m, r;
			l = sb.charAt(i);
			m = sb.charAt(i + 1);
			r = sb.charAt(i + 2);
			char c;// ティツィツ暗ァツョツ療ァツオツ静ヲツ楪? c=l+r, c=l*r
			if (isNum(l) && isNum(r)) {
				if (m == '+') {
					c = (char) (or[l - '0'][r - '0'] + '0');
					sb.setCharAt(i, c);
					sb.delete(i + 1, i + 1 + 2);
				} else if (m == '*') {
					c = (char) (and[l - '0'][r - '0'] + '0');
					sb.setCharAt(i, c);
					sb.delete(i + 1, i + 1 + 2);
				}
			}
		}
	}

	static void removeNest(StringBuilder sb) {
		for (int i = 0; i + 2 < sb.length(); i++) {
			char l, m, r;
			l = sb.charAt(i);
			m = sb.charAt(i + 1);
			r = sb.charAt(i + 2);
			char c;// ティツィツ暗ァツョツ療ァツオツ静ヲツ楪? c=l+r, c=l*r
			if (l == '(' && r == ')' && isNum(m)) {
				sb.delete(i, i + 1);
				sb.delete(i + 1, i + 1 + 1);
			}
		}
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while (true) {
			StringBuilder f = new StringBuilder(in.next());
			StringBuilder origin = new StringBuilder(f.toString());
			if (f.toString().equals(".")) {
				return;
			}
			int ans = 0;
			for (int p = 0; p <= 2; p++) {
				for (int q = 0; q <= 2; q++) {
					for (int r = 0; r <= 2; r++) {
						f = new StringBuilder(origin.toString());
						System.out.printf("p q r = %d %d %d\n", p, q, r);
						while (f.length() > 1) {
							replace(f, p, q, r);

							negRemove(f);

							negate(f);

							calc(f);

							removeNest(f);

						}

						if (f.charAt(0) == '2') {
							ans++;
						}
					}
				}
			}
			System.out.println(ans);
		}
	}
}