Language: C++

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

#define MAX_N 10000

int bit(int c,int d);
int minus(int c);
int kake(int c,int d);
int plus(int c,int d);
string tstring(int c);

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

int bit(int c,bool d){
	if(d==true){return c;}
	return minus(c);
}
int minus(int c){return 2-c;}
int plus(int c,int d){return max(c,d);}
int kake(int c,int d){return max(c,d);}

string tstring(int c)
{
	if(c==0)
	{
		return "0";
	}
	if(c==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];
						}
					}
					sum+=kaiseki(T);
				}
			}
		}
		cout<<sum<<endl;
	}
}

Compiler output:
a.cc: In function 'int bit(int, bool)':
a.cc:21:16: error: reference to 'minus' is ambiguous
   21 |         return minus(c);
      |                ^~~~~
In file included from /usr/include/c++/14/string:49,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_function.h:163:12: note: candidates are: 'template<class _Tp> struct std::minus'
  163 |     struct minus;
      |            ^~~~~
a.cc:9:5: note:                 'int minus(int)'
    9 | int minus(int c);
      |     ^~~~~
a.cc: In function 'int kaiseki2(std::string)':
a.cc:95:28: error: reference to 'plus' is ambiguous
   95 |         if(op=='+'){return plus(ans,ans2);}
      |                            ^~~~
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:24:5: note:                 'int plus(int, int)'
   24 | int plus(int c,int d){return max(c,d);}
      |     ^~~~