Cod sursa(job #2671822)

Utilizator Casian_doispeChiriac Casian Casian_doispe Data 12 noiembrie 2020 18:36:27
Problema Bool Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.73 kb
#include <fstream>
#include <deque>
#include <vector>
#include <bitset>
#include <queue>
#include <unordered_set>
#include <algorithm>
#include <cmath>
#include <limits.h>
#include <cstring>

#define MOD 20173333

using namespace std ;

ifstream cin ("bool.in") ;
ofstream cout ("bool.out") ;

bool is_false(char *ptr)
{

    if((ptr[0] == 'F') &&
    (ptr[1] == 'A') &&
    (ptr[2] == 'L') &&
    (ptr[3] == 'S') &&
    (ptr[4] == 'E'))return 1 ;

    return 0 ;

}

bool is_true(char *ptr)
{

    if((ptr[0] == 'T') &&
    (ptr[1] == 'R') &&
    (ptr[2] == 'U') &&
    (ptr[3] == 'E'))return 1 ;

    return 0 ;

}

bool frecv[500] ;

bool rez(int a, int b, int semn)
{

    if(semn == '|')
    {

        if(a == '0' && b == '0')return 0 ;

        return 1 ;

    }
    else
    {

        if(a == '0' || b == '0')return 0 ;

        return 1 ;

    }

}

bool real_eval(string a)
{
    string aux ;

    for(int f = 0 ; f < a.size() ; f ++)
        if(a[f] == '!')a[f+1] = !(a[f + 1] - '0') + '0' ;

    for(int f = 0 ; f < a.size() ; f ++)
        if(a[f] != '!')aux += a[f] ;

    a = aux ;

    int rezz, semn ;

    for(int f = 0 ; f < a.size() ; f ++)
    {

        if(strchr("|&", a[f]))
        {

            semn = a[f] ;

        }
        else
        {

            if(f)
            {

                rezz = rez(a[f], rezz, semn) + '0' ;

            }
            else rezz = a[f] ;

        }

    }

    return rezz == '1' ;

}

bool evaluare(string a)
{

    deque<int> numere ;

    char *ptr = strtok(&a[0], " ") ;

    while(ptr)
    {

        string auxx = ptr ;

        if(auxx == "AND")numere.push_back('&') ;
            else if(auxx == "OR")numere.push_back('|') ;
                else if(auxx == "NOT")numere.push_back('!') ;
                else
                {

                    int n = strlen(ptr) ;

                    for(int f = 0 ; f < n ; f ++)
                    {

                        if(ptr[f] == '(')numere.push_back('(') ;
                            else if(ptr[f] == ')')
                            {

                                string aux ;

                                while(numere.back() != '(')
                                        aux += numere.back(), numere.pop_back() ;

                                reverse(aux.begin(), aux.end()) ;

                                numere.pop_back() ;

                                numere.push_back(real_eval(aux) + '0') ;

                            }
                            else if(is_false(&ptr[f]))
                            {

                                numere.push_back('0') ;

                                f += 4 ;

                            }
                            else if(is_true(&ptr[f]))
                            {

                                numere.push_back('1') ;

                                f += 3 ;

                            }
                            else
                            {

                                numere.push_back(frecv[ptr[f]] + '0') ;

                            }

                    }

                }

        ptr = strtok(0, " ") ;

    }

    string aux ;

    while(numere.size())
    {

        aux += numere.back() ;

        numere.pop_back() ;

    }

    reverse(aux.begin(), aux.end()) ;

    return real_eval(aux) ;

}

int main()
{

    string a ;

    getline(cin, a) ;

    int n ;

    cin >> n ;

    string b ;

    cin >> b ;

    for(int f = 0 ; f < n ; f ++)
    {

        frecv[b[f]] = !frecv[b[f]] ;

        cout << evaluare(a) ;

    }

    return 0;
}