Pagini recente » Cod sursa (job #1633933) | Cod sursa (job #591539) | Cod sursa (job #1452275) | Cod sursa (job #1541133) | Cod sursa (job #1901664)
#include <bits/stdc++.h>
using namespace std;
ifstream in("bool.in");
ofstream out("bool.out");
const int NMAX = 1000;
vector<string> ops;
string mods, line, ans;
int pos;
bool val[30];
int N;
void change() {
for( int i = 0; i < (int)line.size(); ++i ) {
if( line[i] == '(' ) ops.push_back( "(" );
else if( line[i] == ')' ) ops.push_back( ")" );
else if( std::isalpha( line[i] ) ) {
string op;
while( i < (int)line.size() && std::isalpha( line[i] ) ) {
op.push_back( line[i++] );
}
ops.push_back( op );
--i;
}
}
}
bool eval();
bool and_term() {
bool ans = 0;
if( ops[pos] == "NOT" ) {
++pos;
ans = 1 - eval();
}
else if( ops[pos] == "(" ) {
++pos;
ans = eval();
++pos;
}
else ans = val[ ops[pos].back() - 'A' ];
return ans;
}
bool or_term() {
bool ans = and_term();
while( pos < (int)ops.size() && ops[pos] == "AND" ) {
++pos;
ans = ( ans && and_term() );
}
return ans;
}
bool eval() {
bool ans = or_term();
while( pos < (int)ops.size() && ops[pos] == "OR" ) {
++pos;
ans = (ans || or_term());
}
return ans;
}
int main()
{
getline( in, line, '\n' );
change();
///out << line << '\n';
in >> N >> mods;
for( auto ch : mods ) {
val[ ch - 'A' ] ^= 1;
pos = 0;
ans.push_back( '0' + eval() );
}
out << ans << '\n';
return 0;
}