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

Submitted solutions:

code_snippet: #include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a,b,c,ch[80],ans,A,B,C,D;
string s;
int no(int n){
    if(n==0)return 2;
    if(n==1)return 1;
    return 0;
}
int h(int x,int y){
    if(x*y==0)return 0;
    if(x*y==4)return 2;
    return 1;
}
int H(int x,int y){
    if(x==2||y==2)return 2;
    if(x==0&&y==0)return 0;
    return 1;
}
int f(void){
    int cnt=0;
    stack<int> sta[80];
    stack<int> sta2[80];
    for(int i=0;i<s.size();i++){
        if(s[i]=='(')cnt++;
        if(s[i]=='-')ch[cnt]^=1;
        if(s[i]=='P'){
            if(ch[cnt]){ch[cnt]=0;sta[cnt].push(no(a));}
            else sta[cnt].push(a);
        }
        if(s[i]=='Q'){
            if(ch[cnt]){ch[cnt]=0;sta[cnt].push(no(b));}
            else sta[cnt].push(b);
        }
        if(s[i]=='R'){
            if(ch[cnt]){ch[cnt]=0;sta[cnt].push(no(c));}
            else sta[cnt].push(c);
        }
        if(s[i]=='0'){
            if(ch[cnt]){ch[cnt]=0;sta[cnt].push(no(0));}
            else sta[cnt].push(0);
        }
        if(s[i]=='1'){
            if(ch[cnt]){ch[cnt]=0;sta[cnt].push(no(1));}
            else sta[cnt].push(1);
        }
        if(s[i]=='2'){
            if(ch[cnt]){ch[cnt]=0;sta[cnt].push(no(2));}
            else sta[cnt].push(2);
        }
        if(s[i]=='+')sta2[cnt].push(0);
        if(s[i]=='*')sta2[cnt].push(1);
        if(s[i]==')'){
            while(sta[cnt].size()>1){
                A=sta[cnt].top();
                sta[cnt].pop();
                B=sta[cnt].top();
                sta[cnt].pop();
                C=sta2[cnt].top();
                sta2[cnt].pop();
                if(C==0)D=H(A,B);
                if(C==1)D=h(A,B);
                sta[cnt].push(D);
            }
            if(sta[cnt].size()==1){
                A=sta[cnt].top();
                sta[cnt].pop();
                if(ch[cnt-1]){ch[cnt-1]=0;sta[cnt-1].push(no(A));}
                else sta[cnt-1].push(A);
            }
            cnt--;
        }
    }
    return sta[0].top();
}
int main(void){
    while(1){
        cin>>s;
        for(int i=0;i<80;i++)ch[i]=0;
        if(s==".")return 0;
        ans=0;
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                for(int k=0;k<3;k++){
                    a=i,b=j,c=k;
                    //cout<<i<<" "<<j<<" "<<k<<" "<<f()<<endl;
                    //cout<<(H(h(a,b),c))<<endl;
                    if(f()==2)ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3432
final_rank: 10

code_snippet: #include <bits/stdc++.h>

using namespace std;

int add(int a, int b){
	return max(a,b);
}

int mul(int a, int b){
	return min(a,b);
}

int sub(int a){
	return 2-a;
}

bool is_digit(char c){
	return c >= '0' && c <= '9';
}

string calc(string s, int p, int q, int r){
	string ret;
	replace(s.begin(),s.end(),'P',char(p + '0'));
	replace(s.begin(),s.end(),'Q',char(q + '0'));
	replace(s.begin(),s.end(),'R',char(r + '0'));
	//cout << s << endl;
	for(int i=0;i < s.size();i++){
		if(i < s.size()-2){
			if(s[i] == '(' && s[i+2] == ')'){
				ret += s[i+1];
				i+=2;
				continue;
			}
			if(s[i+1] == '+' && is_digit(s[i]) && is_digit(s[i+2])){
				ret += to_string(add(int(s[i] - '0'),int(s[i+2] - '0')));
				i+=2;
				continue;
			}
			if(s[i+1] == '*' && is_digit(s[i]) && is_digit(s[i+2])){
				ret += to_string(mul(int(s[i] - '0'),int(s[i+2] - '0')));
				i+=2;
				continue;
			}
		}
		if(i < s.size()-1){
			if(s[i] == '-' && s[i+1] == '-'){
				i++;
				continue;
			}
			if(s[i] == '-' && is_digit(s[i+1])){
				ret += to_string(sub(int(s[i+1] - '0')));
				i++;
				continue;
			}
		}
		ret += s[i];
	}
	//cout << ret << endl;
	if(ret.size() > 1){
		return calc(ret, p, q, r);
	} else {
		return ret;
	}
}

int main(){
	string s;
	while(cin >> s, s != "."){
		int ans = 0;
		for(int p=0;p<3;p++){
			for(int q=0;q<3;q++){
				for(int r=0;r<3;r++){
					int x = stoi(calc(s, p, q, r));
					if(x == 2){
						ans ++;
					}
				}
			}
		}
		cout << ans << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3228
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

int solve(string s, int p, int q, int r){
    s=regex_replace(s,regex("P"),to_string(p));
    s=regex_replace(s,regex("Q"),to_string(q));
    s=regex_replace(s,regex("R"),to_string(r));
    string a="";
    for(auto c:s){
        int n="012"s.find(c);
        if(n!=-1 and a.back()=='-'){
            a.back()="210"[n];
        }else{
            a+=c;
        }
        if(c==')'){
            auto f=a.substr(0,a.size()-5);
            auto p=a.substr(a.size()-5);
            if(f.back()=='-'){
                if(p[2]=='+'){
                    a=f.substr(0,f.size()-1)+"210"["012"s.find(max(p[1],p[3]))];
                }else{
                    a=f.substr(0,f.size()-1)+"210"["012"s.find(min(p[1],p[3]))];
                }
            }else{
                if(p[2]=='+'){
                    a=f+max(p[1],p[3]);
                }else{
                    a=f+min(p[1],p[3]);
                }
            }
        }
    }
    return a=="2"?1:0;
}

int main(void){
    string s;
    while(cin>>s,s!="."){
        auto cp=regex_replace(s,regex("--"),"");
        int ans=0;
        rep(p,3)rep(q,3)rep(r,3){
            ans+=solve(cp,p,q,r);
        }
        cout<<ans<<endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3220
final_rank: 9

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;


#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010

int c_not[3]    = {2,1,0};
int c_and[3][3] = {{0,0,0},{0,1,1},{0,1,2}};
int c_or[3][3]  = {{0,1,2},{1,1,2},{2,2,2}};

int calc(string s, int p, int q, int r){

  for(int i=0;i<s.size();i++){

    if(s[i] == 'P'){
      s[i] = '0' + p;
    }
    if(s[i] == 'Q'){
      s[i] = '0' + q;
    }
    if(s[i] == 'R'){
      s[i] = '0' + r;
    }
    
  }
  
  while(s.size() > 1){
    string s_old = s;
    
    //debug(s);
    
    for(int i=0;i<s.size();i++){

      //not
      if(i+1 < s.size()){
        if(s[i] == '-' && ('0' <= s[i+1] && s[i+1] <= '2')){
          int p = c_not[s[i+1]-'0'];
          s = s.substr(0,i) + "0" + s.substr(i+2, s.size() - (i+2));
          s[i] += p;
          break;
        }
      }

      
      if(0 < i && i+1 < s.size()){
        //and
        if(('0' <= s[i-1] && s[i-1] <= '2') &&
           s[i] == '*' &&
           ('0' <= s[i+1] && s[i+1] <= '2')){
          int p = c_and[s[i-1]-'0'][s[i+1]-'0'];
          s = s.substr(0,i-1) + "0" + s.substr(i+2, s.size() - (i+2));
          s[i-1] += p;
          break;
        }

        //or
        if(('0' <= s[i-1] && s[i-1] <= '2') &&
           s[i] == '+' &&
           ('0' <= s[i+1] && s[i+1] <= '2')){
          int p = c_or[s[i-1]-'0'][s[i+1]-'0'];
          s = s.substr(0,i-1) + "0" + s.substr(i+2, s.size() - (i+2));
          s[i-1] += p;
          break;
        }

        //(*)
        if(s[i-1] == '(' &&
           ('0' <= s[i] && s[i] <= '2') &&
           s[i+1] == ')'){
          int p = s[i]-'0';
          s = s.substr(0,i-1) + "0" + s.substr(i+2, s.size() - (i+2));
          s[i-1] += p;
          break;
        }
      }
    }

    if(s == s_old){
      assert(false);
      break;
    }
  }

  return s[0]-'0';
}

bool solve(){
  string s;

  cin >> s;

  if(s == ".") return false;

  int ans = 0;
  
  for(int i=0;i<=2;i++){
    for(int j=0;j<=2;j++){
      for(int k=0;k<=2;k++){
        ans += calc(s,i,j,k) == 2;
      }
    }
  }

  printf("%d\n",ans);

  return true;
}
  

int main(){

  while(solve());
  
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3212
final_rank: 6

code_snippet: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <stack>
   
using namespace std;

int negation(int x)
{
    if(x == 0) return 2;
    if(x == 2) return 0;
    return 1;
}

int conjuction(int x, int y)
{
    if(x == 2 && y == 2) return 2;
    else if(x != 0 && y != 0) return 1;
    else return 0; 
}

int disjunction(int x, int y){
    if(x == 2 || y == 2) return 2;
    else if(x == 1 || y == 1) return 1;
    else return 0;
}



int solve(string s, int p, int q, int r)
{
    stack<int> st;
    stack<char> op;
    for(int i = 0; i < s.length(); i++){
        if(s[i] == '-' || s[i] == '+' || s[i] == '*'){
            op.push(s[i]);
        } else {
            int tmp1, tmp2;
            if(s[i] == '('){
                i++;
                string news = "";
                int kakkonum = 1;
                while(1){
                    // cout << i << endl;
                    if(s[i] == ')') kakkonum--;
                    else if(s[i] == '(') kakkonum++;
                    if(kakkonum == 0) break;
                    news += s[i];
                    i++;
                }
                // cout << news << endl;
                st.push(solve(news, p, q, r));
            } else if(isdigit(s[i])){
                st.push(s[i] - '0');
            } else {
                if(s[i] == 'P') st.push(p);
                else if(s[i] == 'Q') st.push(q);
                else st.push(r);
            }
            while(!op.empty() && op.top() == '-'){
                op.pop();
                tmp1 = st.top();
                st.pop();
                st.push(negation(tmp1));
            }
            if(!op.empty() && op.top() == '+'){
                op.pop();
                tmp1 = st.top();
                st.pop();
                tmp2 = st.top();
                st.pop();
                st.push(disjunction(tmp1, tmp2));
            } else if(!op.empty() && op.top() == '*'){
                op.pop();
                tmp1 = st.top();
                st.pop();
                tmp2 = st.top();
                st.pop();
                st.push(conjuction(tmp1, tmp2));
            }
        }
    }
    // cout << s  << " " << p << " " << q << " " << r << " " << st.top() << endl;
    return st.top();
}

int main()
{
    string s;
    while(1){
        cin >> s;
        if(s == ".") break;
        int ans = 0;
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                for(int k = 0; k < 3; k++){
                    if(solve(s, i, j, k) == 2) ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1212
final_rank: 8

code_snippet: #include<iostream>
#include<string>
using namespace std;
int o=0;
void rec(string str){
  //cout << str << endl;
  string next,buf;
  if('0'<=str[0]&&str[0]<='2'){
    //cout << str << endl;
    if(str[0]=='2') o++;
    return;
  }
  for(int i=1;i<str.size();i++){
    if('0'<=str[i]&&str[i]<='2') {
      if(str[i-1]=='-') {
	if(str[i]=='0')  str[i]='2';
	else if(str[i]=='2')  str[i]='0';
	str[i-1]=' ';
      }else if(str[i-1]=='*') {
	if(str[i-3]=='('){
	if(str[i]=='0'||str[i-2]=='0') {
	  str[i-3]=' ';
	  str[i-2]=' ';
	  str[i-1]=' ';
	  str[i]='0';
	  str[i+1]=' ';
	}else if(str[i]=='2'&&str[i-2]=='2') {
	  str[i-3]=' ';
	  str[i-2]=' ';
	  str[i-1]=' ';
	  str[i]='2';
	  str[i+1]=' ';
	}else if('0'<=str[i-2]&&str[i-2]<='2'){
	  str[i-3]=' ';
	  str[i-2]=' ';
	  str[i-1]=' ';
	  str[i]='1';
	  str[i+1]=' ';
	}
	}
      }else if(str[i-1]=='+') {
	if(str[i-3]=='('){
	if(str[i]=='2'||str[i-2]=='2') {
	  str[i-3]=' ';
	  str[i-2]=' ';
	  str[i-1]=' ';
	  str[i]='2';
	  str[i+1]=' ';
	}else if(str[i]=='0'&&str[i-2]=='0') {
	  str[i-3]=' ';
	  str[i-2]=' ';
	  str[i-1]=' ';
	  str[i]='0';
	  str[i+1]=' ';
	}else if('0'<=str[i-2]&&str[i-2]<='2'){
	  str[i-3]=' ';
	  str[i-2]=' ';
	  str[i-1]=' ';
	  str[i]='1';
	  str[i+1]=' ';
	}
	}
      }
    }
  }
  for(int i=0;i<str.size();i++){
    if(str[i]!=' ') next+=str[i];
  }
  rec(next);
}
int main(){
  string str,buf;
  cin >> str;
  while(str!="."){
    int p[]={0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2};
    int q[]={0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2};
    int r[]={0,0,0,1,1,1,2,2,2,0,0,0,1,1,1,2,2,2,0,0,0,1,1,1,2,2,2};
    o=0;
    for(int i=0;i<27;i++){
      buf=str;
      for(int j=0;j<str.size();j++){
	if(buf[j]=='P') buf[j]=p[i]+'0';
	if(buf[j]=='Q') buf[j]=q[i]+'0';
	if(buf[j]=='R') buf[j]=r[i]+'0';
      }
      rec(buf);
    }
    cout << o << endl;
    cin >> str;
  }
  
}
accuracy: 1
time_ms: 10
memory_kb: 1200
final_rank: 3

code_snippet: #include<iostream>
#include<string>
using namespace std;

string replacePQR(int p,int q,int r,string input){
  p += '0';
  q += '0';
  r += '0';
  for(int i=0;i<(int)input.size();i++){
    switch(input[i]){
    case 'P':
      input[i]=p;
      break;
    case 'Q':
      input[i]=q;
      break;
    case 'R':
      input[i]=r;
      break;
    }
  }
  return input;
}

string solve(string input){
  //  cout<<input<<endl;  
  if((int)input.size()==1) return input;
  if(input[0]=='('){
    int paren=0;
    int i;
    char op;
    for(i=1;i<(int)input.size();i++){
      if(input[i]=='(') paren++;
      else if(input[i]==')') paren--;
      else if((input[i]=='+' || input[i]=='*' )&& paren == 0){
	op=input[i];
	break;
      }
    }
    string s1,s2;
    s1=solve(input.substr(1,i-1));
    s2=solve(input.substr(i+1,(int)input.size()-2-i));
    if(op=='+'){
      if(s1[0]=='2' || s2[0]=='2') return string(1,'2');
      else if(s1[0]=='1' || s2[0]=='1') return string(1,'1');
      else return string(1,'0');
    }else{
      if(s1[0]=='0' || s2[0]=='0') return string(1,'0');
      else if(s1[0]=='1' || s2[0]=='1') return string(1,'1');
      else return string(1,'2');
    }
  }
  else{
    string s=solve(input.substr(1));
    if(s[0]=='0') s[0]+=2;
    else if(s[0]=='2') s[0]-=2;
    //cout<<" (--"<<s<<"--)"<<endl;
    return s;
  }

}

int main(){
  string input;
  while(cin>>input,input!="."){
    int cnt=0;
    for(int p=0;p<3;p++){
      for(int q=0;q<3;q++){
	for(int r=0;r<3;r++){
	  string s;
	  if((s=solve(replacePQR(p,q,r,input)))=="2") cnt++;
	  //cout<<s<<endl;
	}
      }
    }
    cout<<cnt<<endl;
    
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1196
final_rank: 2

code_snippet: #include<iostream>
#include<string>
#include<algorithm>
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;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1208
final_rank: 4

code_snippet: #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;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1220
final_rank: 5

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

typedef string::iterator State;
typedef map<char, int> Env;

int formula(State& state, Env env) {
    if ('0' <= *state && *state <= '2') {
        return *state++ - '0';
    }

    if ('P' <= *state && *state <= 'R') {
        return env[*state++];
    }

    if (*state == '-') {
        ++state;
        return 2 - formula(state, env);
    }

    ++state; // '('
    int lhs = formula(state, env);
    char op = *state++;
    int rhs = formula(state, env);
    ++state; // ')'

    if (op == '+') {
        return max(lhs, rhs);
    } else if (op == '*') {
        return min(lhs, rhs);
    }

    assert(false);
}


int main() {
    string expr;
    Env env;
    while (true) {
        cin >> expr;

        if (expr == ".") {
            break;
        }

        int count = 0;

        for (int j = 0; j < 27; ++j) {
            env['P'] = j % 3;
            env['Q'] = (j / 3) % 3;
            env['R'] = j / 9;

            State state = expr.begin();
            if (formula(state, env) == 2) {
                ++count;
            }
        }
        
        cout << count << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1188
final_rank: 1