Skip to main content
added 12 characters in body
Source Link
Andrei Rost
  • 195
  • 1
  • 10

This is my implementation; any feedback/suggestions would be very much appreciated.

This is my implementation; any feedback would be very much appreciated.

This is my implementation; any feedback/suggestions would be very much appreciated.

deleted 8 characters in body
Source Link
Andrei Rost
  • 195
  • 1
  • 10
#include <iostream>
#include <string>
#include <algorithm>

inline bool charToBool (const char& c1) {
    if(c1 == '1') return true;
    else return false;
}

// the par function recursively calls itself such that it can
// "create" every possible parenthesisation around every operation.
// Next, it counts the number of boolean values that were calculated

// returns count of true possibile booleans,
// respectively count of false possibile booleans,
// in all iterations of paranthesisation of
// str expression from [first, last)

std::pair<int, int> par(const std::string& str, const int first, const int last) {

    // substrLen is the length of the current parenthesis

    int substrLen = last-first;

    // base condition

    if(substrLen == 1){
        if(charToBool(str[first])) return {1, 0};
        else return {0, 1};
    }

    std::pair<int, int> total = {0, 0};

    // function par iterates through all possible parenthesis lengths
    // from index first, then recursively calls itself
    // for the rest of said expression str

    for(int len = 1; len <= substrLen-2; len+=2) {

        // pairs left and right represent the left parenthesis,
        // respectively right parenthesis of expressions,
        // compared to the token of operation

        std::pair<int, int> left = par(str, first, first+len);
        std::pair<int, int> right = par(str, first+len+1, last);

        switch(str[first+len]) {

            case '|':

                total.first += left.first*right.second + left.second*right.first + left.first*right.first;
                total.second += left.second*right.second;
                break;

            case '&':

                total.first += std::min(left.first, right.first);
                total.second += left.first*right.second + left.second*right.first + left.second*right.second;
                break;

            case '^':

                total.first += left.first*right.second + left.second*right.first;
                total.second +=  left.first*right.first + left.second*right.second;
                break;
        }
    }
    return total;
}

// the countEval function returns count
// of target bool, after parenthesizing
// through the par function

int countEval(const std::string& str, const bool& target) {

    // checks if string is valid
    // (it must be of uneven size)

    if(str.size() % 2 == 0) return 0;

    std::pair<int, int> res = par(str, 0, str.size());

    // first number of pair res is the true number count
    // second number of pair res is the false number count

    if(target) return res.first;
    else return res.second;
}

int main() {

    const std::string str = "0&0&0&1^1|0";
    int res = countEval(str, true);

    std::cout << "res = " << res << '\n';

    return 0;
}

// TIME COMPLEXITY: O(n^3);
// SPACE COMPLEXITY : o(n^2);
// where n is the length of std::string str
#include <iostream>
#include <string>
#include <algorithm>

inline bool charToBool (const char& c1) {
    if(c1 == '1') return true;
    else return false;
}

// the par function recursively calls itself such that it can
// "create" every possible parenthesisation around every operation.
// Next, it counts the number of boolean values that were calculated

// returns count of true possibile booleans,
// respectively count of false possibile booleans,
// in all iterations of paranthesisation of
// str expression from [first, last)

std::pair<int, int> par(const std::string& str, const int first, const int last) {

    // substrLen is the length of the current parenthesis

    int substrLen = last-first;

    // base condition

    if(substrLen == 1){
        if(charToBool(str[first])) return {1, 0};
        else return {0, 1};
    }

    std::pair<int, int> total = {0, 0};

    // function par iterates through all possible parenthesis lengths
    // from index first, then recursively calls itself
    // for the rest of said expression str

    for(int len = 1; len <= substrLen-2; len+=2) {

        // pairs left and right represent the left parenthesis,
        // respectively right parenthesis of expressions,
        // compared to the token of operation

        std::pair<int, int> left = par(str, first, first+len);
        std::pair<int, int> right = par(str, first+len+1, last);

        switch(str[first+len]) {

            case '|':

                total.first += left.first*right.second + left.second*right.first + left.first*right.first;
                total.second += left.second*right.second;
                break;

            case '&':

                total.first += std::min(left.first, right.first);
                total.second += left.first*right.second + left.second*right.first + left.second*right.second;
                break;

            case '^':

                total.first += left.first*right.second + left.second*right.first;
                total.second +=  left.first*right.first + left.second*right.second;
                break;
        }
    }
    return total;
}

// the countEval function returns count
// of target bool, after parenthesizing
// through the par function

int countEval(const std::string& str, const bool& target) {

    // checks if string is valid
    // (it must be of uneven size)

    if(str.size() % 2 == 0) return 0;

    std::pair<int, int> res = par(str, 0, str.size());

    // first number of pair res is the true number count
    // second number of pair res is the false number count

    if(target) return res.first;
    return res.second;
}

int main() {

    const std::string str = "0&0&0&1^1|0";
    int res = countEval(str, true);

    std::cout << "res = " << res << '\n';

    return 0;
}

// TIME COMPLEXITY: O(n^3);
// SPACE COMPLEXITY : o(n^2);
// where n is the length of std::string str
#include <iostream>
#include <string>
#include <algorithm>

inline bool charToBool (const char& c1) {
    if(c1 == '1') return true;
    else return false;
}

// the par function recursively calls itself such that it can
// "create" every possible parenthesisation around every operation.
// Next, it counts the number of boolean values that were calculated

// returns count of true possibile booleans,
// respectively count of false possibile booleans,
// in all iterations of paranthesisation of
// str expression from [first, last)

std::pair<int, int> par(const std::string& str, const int first, const int last) {

    // substrLen is the length of the current parenthesis

    int substrLen = last-first;

    // base condition

    if(substrLen == 1){
        if(charToBool(str[first])) return {1, 0};
        else return {0, 1};
    }

    std::pair<int, int> total = {0, 0};

    // function par iterates through all possible parenthesis lengths
    // from index first, then recursively calls itself
    // for the rest of said expression str

    for(int len = 1; len <= substrLen-2; len+=2) {

        // pairs left and right represent the left parenthesis,
        // respectively right parenthesis of expressions,
        // compared to the token of operation

        std::pair<int, int> left = par(str, first, first+len);
        std::pair<int, int> right = par(str, first+len+1, last);

        switch(str[first+len]) {

            case '|':

                total.first += left.first*right.second + left.second*right.first + left.first*right.first;
                total.second += left.second*right.second;
                break;

            case '&':

                total.first += std::min(left.first, right.first);
                total.second += left.first*right.second + left.second*right.first + left.second*right.second;
                break;

            case '^':

                total.first += left.first*right.second + left.second*right.first;
                total.second +=  left.first*right.first + left.second*right.second;
                break;
        }
    }
    return total;
}

// the countEval function returns count
// of target bool, after parenthesizing
// through the par function

int countEval(const std::string& str, const bool& target) {

    // checks if string is valid
    // (it must be of uneven size)

    if(str.size() % 2 == 0) return 0;

    std::pair<int, int> res = par(str, 0, str.size());

    // first number of pair res is the true number count
    // second number of pair res is the false number count

    if(target) return res.first;
    else return res.second;
}

int main() {

    const std::string str = "0&0&0&1^1|0";
    int res = countEval(str, true);

    std::cout << "res = " << res << '\n';

    return 0;
}

// TIME COMPLEXITY: O(n^3);
// SPACE COMPLEXITY : o(n^2);
// where n is the length of std::string str
deleted 8 characters in body
Source Link
Andrei Rost
  • 195
  • 1
  • 10
#include <iostream>
#include <string>
#include <algorithm>

inline bool charToBool (const char& c1) {
    if(c1 == '1') return true;
    else return false;
}

// the par function recursively calls itself such that it can
// "create" every possible parenthesisation around every operation.
// Next, it counts the number of boolean values that were calculated

// returns count of true possibile booleans,
// respectively count of false possibile booleans,
// in all iterations of paranthesisation of
// str expression from [first, last)

std::pair<int, int> par(const std::string& str, const int first, const int last) {

    // substrLen is the length of the current parenthesis

    int substrLen = last-first;

    // base condition

    if(substrLen == 1){
        if(charToBool(str[first])) return {1, 0};
        else return {0, 1};
    }

    std::pair<int, int> total = {0, 0};

    // function par iterates through all possible parenthesis lengths
    // from index first, then recursively calls itself
    // for the rest of said expression str

    for(int len = 1; len <= substrLen-2; len+=2) {

        // pairs left and right represent the left parenthesis,
        // respectively right parenthesis of expressions left,
        // respectivelycompared rightto ofthe saidtoken expressionof operation

        std::pair<int, int> left = par(str, first, first+len);
        std::pair<int, int> right = par(str, first+len+1, last);

        switch(str[first+len]) {

            case '|':

                total.first += left.first*right.second + left.second*right.first + left.first*right.first;
                total.second += left.second*right.second;
                break;

            case '&':

                total.first += std::min(left.first, right.first);
                total.second += left.first*right.second + left.second*right.first + left.second*right.second;
                break;

            case '^':

                total.first += left.first*right.second + left.second*right.first;
                total.second +=  left.first*right.first + left.second*right.second;
                break;
        }
    }
    return total;
}

// the countEval function returns count
// of target bool, after parenthesizing
// through the par function

int countEval(const std::string& str, const bool& target) {

    // checks if string is valid
    // (it must be of uneven size)

    if(str.size() % 2 == 0) return 0;

    std::pair<int, int> res = par(str, 0, str.size());

    // first number of pair res is the true number count
    // second number of pair res is the false number count

    if(target) return res.first;
    return res.second;
}

int main() {

    const std::string str = "0&0&0&1^1|0";
    int res = countEval(str, true);

    std::cout << "res = " << res << '\n';

    return 0;
}

// TIME COMPLEXITY: O(n^3);
// SPACE COMPLEXITY : o(n^2);
// where n is the length of std::string str
#include <iostream>
#include <string>
#include <algorithm>

inline bool charToBool (const char& c1) {
    if(c1 == '1') return true;
    else return false;
}

// the par function recursively calls itself such that it can
// "create" every possible parenthesisation around every operation.
// Next, it counts the number of boolean values that were calculated

// returns count of true possibile booleans,
// respectively count of false possibile booleans,
// in all iterations of paranthesisation of
// str expression from [first, last)

std::pair<int, int> par(const std::string& str, const int first, const int last) {

    // substrLen is the length of the current parenthesis

    int substrLen = last-first;

    // base condition

    if(substrLen == 1){
        if(charToBool(str[first])) return {1, 0};
        else return {0, 1};
    }

    std::pair<int, int> total = {0, 0};

    // function par iterates through all possible parenthesis lengths
    // from index first, then recursively calls itself
    // for the rest of said expression str

    for(int len = 1; len <= substrLen-2; len+=2) {

        // pairs left and right represent the left parenthesis,
        // respectively right parenthesis of expressions left,
        // respectively right of said expression

        std::pair<int, int> left = par(str, first, first+len);
        std::pair<int, int> right = par(str, first+len+1, last);

        switch(str[first+len]) {

            case '|':

                total.first += left.first*right.second + left.second*right.first + left.first*right.first;
                total.second += left.second*right.second;
                break;

            case '&':

                total.first += std::min(left.first, right.first);
                total.second += left.first*right.second + left.second*right.first + left.second*right.second;
                break;

            case '^':

                total.first += left.first*right.second + left.second*right.first;
                total.second +=  left.first*right.first + left.second*right.second;
                break;
        }
    }
    return total;
}

// the countEval function returns count
// of target bool, after parenthesizing
// through the par function

int countEval(const std::string& str, const bool& target) {

    // checks if string is valid
    // (it must be of uneven size)

    if(str.size() % 2 == 0) return 0;

    std::pair<int, int> res = par(str, 0, str.size());

    // first number of pair res is the true number count
    // second number of pair res is the false number count

    if(target) return res.first;
    return res.second;
}

int main() {

    const std::string str = "0&0&0&1^1|0";
    int res = countEval(str, true);

    std::cout << "res = " << res << '\n';

    return 0;
}

// TIME COMPLEXITY: O(n^3);
// SPACE COMPLEXITY : o(n^2);
// where n is the length of std::string str
#include <iostream>
#include <string>
#include <algorithm>

inline bool charToBool (const char& c1) {
    if(c1 == '1') return true;
    else return false;
}

// the par function recursively calls itself such that it can
// "create" every possible parenthesisation around every operation.
// Next, it counts the number of boolean values that were calculated

// returns count of true possibile booleans,
// respectively count of false possibile booleans,
// in all iterations of paranthesisation of
// str expression from [first, last)

std::pair<int, int> par(const std::string& str, const int first, const int last) {

    // substrLen is the length of the current parenthesis

    int substrLen = last-first;

    // base condition

    if(substrLen == 1){
        if(charToBool(str[first])) return {1, 0};
        else return {0, 1};
    }

    std::pair<int, int> total = {0, 0};

    // function par iterates through all possible parenthesis lengths
    // from index first, then recursively calls itself
    // for the rest of said expression str

    for(int len = 1; len <= substrLen-2; len+=2) {

        // pairs left and right represent the left parenthesis,
        // respectively right parenthesis of expressions,
        // compared to the token of operation

        std::pair<int, int> left = par(str, first, first+len);
        std::pair<int, int> right = par(str, first+len+1, last);

        switch(str[first+len]) {

            case '|':

                total.first += left.first*right.second + left.second*right.first + left.first*right.first;
                total.second += left.second*right.second;
                break;

            case '&':

                total.first += std::min(left.first, right.first);
                total.second += left.first*right.second + left.second*right.first + left.second*right.second;
                break;

            case '^':

                total.first += left.first*right.second + left.second*right.first;
                total.second +=  left.first*right.first + left.second*right.second;
                break;
        }
    }
    return total;
}

// the countEval function returns count
// of target bool, after parenthesizing
// through the par function

int countEval(const std::string& str, const bool& target) {

    // checks if string is valid
    // (it must be of uneven size)

    if(str.size() % 2 == 0) return 0;

    std::pair<int, int> res = par(str, 0, str.size());

    // first number of pair res is the true number count
    // second number of pair res is the false number count

    if(target) return res.first;
    return res.second;
}

int main() {

    const std::string str = "0&0&0&1^1|0";
    int res = countEval(str, true);

    std::cout << "res = " << res << '\n';

    return 0;
}

// TIME COMPLEXITY: O(n^3);
// SPACE COMPLEXITY : o(n^2);
// where n is the length of std::string str
edited tags
Link
G. Sliepen
  • 69.5k
  • 3
  • 75
  • 180
Loading
edited tags; edited title
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Loading
Source Link
Andrei Rost
  • 195
  • 1
  • 10
Loading