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: Accepted)

import java.util.*;

public class Main{

    String line;
    int[][] pqr;
    int idx,cnt;

    void solve(){
        Scanner sc = new Scanner(System.in);

        idx = 0;
        pqr = new int[27][3];
        getPQR(0,new int[3]);

        while(true){
            line = sc.next();
            if(line.equals(".")) break;

            cnt = 0;
            String line2 = line;
            LinkedList<Integer> left = new LinkedList<Integer>();
            for(int i=0; i<27; i++){
                line = line2;
                char[] c = line.toCharArray();
                for(int j=0; j<line.length(); j++){
                    if(c[j]>='P' && c[j]<='R') c[j] = (char)(pqr[i][c[j]-'P']+'0');  
                }
                line = String.valueOf(c);

                for(int j=0; j<line.length(); j++){
                    if(line.charAt(j)=='(') left.add(j);
                    if(line.charAt(j)==')'){
                        int l = left.pollLast();
                        String s = line.substring(l+1,j);
                        String front = line.substring(0,l);
                        String rear = line.substring(j+1,line.length());
                        String newS = getAns(s);
                        line = front + newS + rear;
                        j = j-(s.length()-newS.length())-2;
                    }
                }
                if(getAns(line).equals("2")) cnt++;
            }

            System.out.println(cnt);
        }
    }

    String getAns(String s){
        String x = "",y = "";
        boolean and = false,or = false;
        for(int i=0; i<s.length(); i++){
            if(s.charAt(i)=='*'){
                and = true;
                x = s.substring(0,i);
                y = s.substring(i+1,s.length());
            }else if(s.charAt(i)=='+'){
                or = true;
                x = s.substring(0,i);
                y = s.substring(i+1,s.length());
            }
        }

        if(and){
            return String.valueOf(Math.min(getNum(x),getNum(y)));
        }else if(or){
            return String.valueOf(Math.max(getNum(x),getNum(y)));
        }else{
            return String.valueOf(getNum(s));
        }
    }

    int getNum(String s){
        int ii = 0;
        while(s.charAt(ii)=='-') ii++;

        int n = Integer.parseInt(s.substring(ii,s.length()));
        for(int i=0; i<ii; i++) n = 2-n;
  
        return n;
    }

    void getPQR(int pos,int[] num){
        if(pos==3){
            for(int i=0; i<3; i++) pqr[idx][i] = num[i];
            idx++;
            return;
        }

        for(int i=0; i<3; i++){
            num[pos] = i;
            getPQR(pos+1,num);
        }
    }

    public static void main(String[] args){
        new Main().solve();
    }
}