Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9037349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:13:34+00:00 2026-06-16T09:13:34+00:00

I have a logical expression that I would like to evaluate. The expression can

  • 0

I have a logical expression that I would like to evaluate.
The expression can be nested and consists of T (True) or F (False) and parenthesis.
The parenthesis “(” means “logical OR”.
Two terms TF beside each others (or any other two combinations beside each others), should be ANDED (Logical AND).

For example, the expression:

((TFT)T) = true

I need an algorithm for solving this problem. I thought of converting the expression first to disjunctive or conjunctive normal form and then I can easily evaluate the expression. However, I couldn’t find an algorithm that normalizes the expression. Any suggestions? Thank you.

The problem statement can be found here:
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=2&category=378&page=show_problem&problem=2967

Edit: I misunderstood part of the problem. In the given logical expression, the AND/OR operators alternate with every parenthesis “(“. If we are to represent the expression by a tree, then the AND/OR operators depend on the the sub-tree’s depth-level. However, it’s initially given that the trees at the deepest level are AND-trees. My task is to evaluate the given expression possibly by constructing the tree.
Thanks for the answers below which clarified the correct requirement of the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:13:35+00:00Added an answer on June 16, 2026 at 9:13 am

    I solved this problem using a different technique than the ones mentioned. And I got it Accepted by the online system judge.

    After figuring out the operator at the first level of the tree (Thanks to @Asiri Rathnayake for his idea), I recursively construct the expression tree. During the construction, I scan the string. If the character is ‘(‘, then I create a node with the current operator value and add it to the tree. Then, I alternate the operator and go for a deeper recursion level. If the character is ‘T’, then I create a node with value “True”, add it to the tree and continue scanning. If the character is ‘F’, then I create a node with the value “False”, add it to the tree and continue scanning. Finally, if the character is ‘)’, then I return to one level up of the recursion.

    At the end, I will have the expression tree completed. Now, all I need to do is a simple evaluation for the tree using basic recursive function.

    Below is my C++ code:

    #include<iostream>
    #include<string>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    
    struct Node {
    
        char value;
        vector<Node*> children;
    };
    
    
    void ConstructTree (int &index, string X, Node *&node, int op)
    {
    
        for(; index<X.size(); index++)
        {
            if(X[index]=='T')
            {
                Node *C= new Node;
                C->value='T';
                node->children.push_back(C);
            }
    
    
            else if(X[index]=='F')
            {
                Node* C= new Node;
                C->value='F';
                node->children.push_back(C);
            }
    
    
            else if(X[index]=='(')
            {
                if(op==0)
                {
                    Node* C= new Node;
                    C->value='O';
                    node->children.push_back(C);
                }
    
    
                else
                {
                    Node* C= new Node;
                    C->value='A';
                    node->children.push_back(C);
                }
    
                index++;
                ConstructTree(index,X,node->children[node->children.size()-1],1-op);
            }
    
            else
                return;
        }
    
    
    
    }
    
    bool evaluateTree(Node* node)
    {
        if(node->value=='T')
            return true;
        else if(node->value=='F')
            return false;
        else if(node->value=='O')
        {
            for(int i=0; i<node->children.size(); i++)
                if(evaluateTree(node->children[i])==true)
                    return true;
    
            return false;
        }
    
        else if(node->value=='A')
        {
    
            for(int i=0; i<node->children.size(); i++)
                if(evaluateTree(node->children[i])==false)
                    return false;
    
            return true;
        }
    }
    
    
    int main()
    {
        string X;
        int testCase=1;
    
        while(cin>>X)
        {
            if(X=="()")
                break;
    
    
            int index=0;
    
            int op=-1;
    
            int P=0;
    
            int max=0;
            for(int i=0; i<X.size(); i++)
            {
                if(X[i]=='(')
                    P++;
                if(X[i]==')')
                    P--;
    
                if(P>max)
                    max=P;
            }
    
    
            if(max%2==0)
                op=0; //OR
            else
                op=1; //AND
    
    
            Node* root = new Node;
    
            if(op==0)
            root->value='O';
            else
            root->value='A';
    
            index++;
            ConstructTree(index,X,root,1-op);
    
            if(evaluateTree(root))
                cout<<testCase<<". true"<<endl;
            else
                cout<<testCase<<". false"<<endl;
    
            testCase++;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have complex logical expression that look like this: ((((!((cond1) || (cond2) || (cond3))
I would like to create a method that accepts an Expression<Func<T, bool>> and creates
I have a report that needs to get data from two logical areas of
I have logical expressions that I need to evaluate. After some expresison template parametrized
I have a C# Console Application project. I have a logical expression that is
I have something of a logical puzzle I can not figure out. I have
i have a many to many table relationship that involves 2 logical tables. Record
My goal is to write a function that will take a logical expression (eg:
Let's say I have a table making a junction between two tables... like this:
what does the operators evaluate on instructions? like: var flag:Boolean=true; flag && trace(1) &&

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.