AOJ 1155 C: 如何に汝を満足せしめむ? いざ数え上げむ…

題名よくわかりませんが...

感想

構文解析入門みたいな感じ。

PQR全通り試していけばいいです。

解析の仕方的には

構文が必ず

  • 「-」+「論理式」
  • 「(」+「論理式」+「op」+「論理式」+「)」
  • 「論理式」

のどれかということを意識してdfs。

#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG_MODE
	#define DBG(n) n;
#else
	#define DBG(n) ;
#endif
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define MOD 1000000007

typedef long long ll;
typedef pair<ll,ll> P;

char tmp[100];
int place;

int m_minus(int num){
	return 2 - num;
}

int m_op(int a,int b,char c){
	if(c == '*')return min(a,b);
	else return max(a,b);
}

int dfs(){
	if(tmp[place] == '-'){
		place++;
		return m_minus(dfs());
	}
	else if(tmp[place] == '('){
		place++;
		int a = dfs();
		char c = tmp[place];
		place++;
		int b = dfs();
		place++;
		return m_op(a,b,c);
	}
	else{
		int ret = tmp[place] - '0';
		place++;
		return ret;
	}
}

int main(){
	
	string str;
	while(cin >> str,str[0] != '.'){			
		int ans = 0;
		
		REP(i,3){
			REP(j,3){
				REP(k,3){
					REP(l,str.size()){
						if(str[l] == 'P'){
							tmp[l] = '0' + i;
						}
						else if(str[l] == 'Q'){
							tmp[l] = '0' + j;
						}
						else if(str[l] == 'R'){
							tmp[l] = '0' + k;
						}
						else tmp[l] = str[l];
					}
					place = 0;
					if(dfs() == 2){
						ans++;
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}