Language: C++

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

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

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);}

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+=to_string((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:13:23: error: class template argument deduction failed:
   13 |         return minus(c);
      |                       ^
a.cc:13:23: error: no matching function for call to 'minus(int&)'
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: candidate: 'template<class _Tp> minus()-> std::minus<_Tp>'
  163 |     struct minus;
      |            ^~~~~
/usr/include/c++/14/bits/stl_function.h:163:12: note:   candidate expects 0 arguments, 1 provided
/usr/include/c++/14/bits/stl_function.h:163:12: note: candidate: 'template<class _Tp> minus(std::minus<_Tp>)-> std::minus<_Tp>'
/usr/include/c++/14/bits/stl_function.h:163:12: note:   template argument deduction/substitution failed:
a.cc:13:23: note:   mismatched types 'std::minus<_Tp>' and 'int'
   13 |         return minus(c);
      |                       ^
a.cc: In function 'int kaiseki2(std::string)':
a.cc:74:28: error: reference to 'plus' is ambiguous
   74 |         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:16:5: note:                 'int plus(int, int)'
   16 | int plus(int c,int d){return max(c,d);}
      |     ^~~~