Cod sursa(job #1901664)

Utilizator felixiPuscasu Felix felixi Data 4 martie 2017 10:14:39
Problema Bool Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
#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;
}