How can I satisfy thee? Let me count the ways...
2008.07.10 22:55 ACM/ICPC 2008
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1155
001 #include<iostream>
002 #include<string>
003 #include<map>
004 #include<cassert>
005 using namespace std;
006
007 map<char, int> V;
008 string exp;
009 int pos;
010
011 int formula(){
012 char ch = exp[pos];
013 if ( isdigit(ch) ){
014 pos++; return ch - '0';
015 } else if ( isalpha(ch) ){
016 pos++; return V[ch];
017 } else if ( ch == '-' ){
018 pos++; return (2 - formula()); // NOT
019 } else if ( ch == '(' ){
020 int v1, v2;
021 char op;
022 pos++;
023 v1 = formula();
024 op = exp[pos++];
025 v2 = formula();
026 assert( exp[pos++] == ')');
027 if ( op == '*' ) return min( v1, v2 ); // AND
028 else if ( op == '+' ) return max( v1, v2 ); // OR
029 }
030 }
031
032 main(){
033 int cnt;
034 while( cin >> exp && exp != "."){
035 cnt = 0;
036 for ( int p = 0; p <= 2; p++ ){
037 for ( int q = 0; q <= 2; q++ ){
038 for ( int r = 0; r <= 2; r++ ){
039 V['P'] = p;
040 V['Q'] = q;
041 V['R'] = r;
042 pos = 0;
043 if ( formula() == 2 ) cnt++;
044 }
045 }
046 }
047 cout << cnt << endl;
048 }
049 }
スポンサーサイト
| コメント(0) | トラックバック(0) | ↑ページトップ |