Boolean Parenthesization-LintCode

204 篇文章 0 订阅

Given a boolean expression with following symbols.

Symbols
    'T' ---> true 
    'F' ---> false 

And following operators filled between symbols

Operators
    &   ---> boolean AND
    |   ---> boolean OR
    ^   ---> boolean XOR 

Count the number of ways we can parenthesize the expression so that the value of expression evaluates to true.

Let the input be in form of two arrays one contains the symbols (T and F) in order and other contains operators (&, | and ^}

样例

Given symbol = ['T', 'F', 'T'], operator = ['^', '&']
return 2
The given expression is "T ^ F & T", it evaluates true, in two ways "((T ^ F) & T)" and "(T ^ (F & T))"

given symbol = ['T', 'F', 'F'], operator = ['^', '|']
return 2
The given expression is "T ^ F | F", it evaluates true, in two ways "( (T ^ F) | F )" and "( T ^ (F | F) )".

思路:
构建vecT[len][len]和vecF[len][len]分别表示字符串从i位置到j位置构成true和false的方式数。

vecT[i][j]=k=ij1totalvecF[i][k]vecF[k+1][j],vecT[i][k]vecF[k+1][j]+vecT[k+1][j]vecF[i][k],vecT[i][k]vecT[k+1][j],oper[k]=|oper[k]=\^other

vecF[i][j]=k=ij1vecF[i][k]vecF[k+1][j],vecT[i][k]vecT[k+1][j]+vecF[k+1][j]vecF[i][k],totalvecT[i][k]vecT[k+1][j],oper[k]=|oper[k]=\^other

#ifndef C725_H
#define C725_H
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
    /*
    * @param symb: the array of symbols
    * @param oper: the array of operators
    * @return: the number of ways
    */
    int countParenth(string &symb, string &oper) {
        // write your code here
        int len = symb.size();
        vector<vector<int>> vecT(len, vector<int>(len, 0));//vecT[i][j]表示在i-j之间的表达式中结果为true的方式数
        vector<vector<int>> vecF(len, vector<int>(len, 0));//vecT[i][j]表示在i-j之间的表达式中结果为false的方式数
        for (int i = 0; i < len; ++i)
        {
            if (symb[i] == 'T')
                vecT[i][i] = 1;
            else
                vecF[i][i] = 1;
        }
        for (int path = 1; path < len; ++path)
        {
            for (int i = 0, j = path; j < len; ++i, ++j)
            {
                for (int n = 0; n < path; ++n)
                {
                    int k = i + n;
                    int total = (vecT[i][k] + vecF[i][k])*(vecT[k + 1][j] + vecF[k + 1][j]);
                    if (oper[k] == '&')
                    {
                        vecT[i][j] += vecT[i][k] * vecT[k + 1][j];
                        vecF[i][j] += total - vecT[i][k] * vecT[k + 1][j];
                    }
                    else if (oper[k] == '|')
                    {
                        vecT[i][j] += total - vecF[i][k] * vecF[k + 1][j];
                        vecF[i][j] += vecF[i][k] * vecF[k + 1][j];
                    }
                    else
                    {
                        vecT[i][j] += vecT[i][k] * vecF[k + 1][j] + vecT[k + 1][j] * vecF[i][k];
                        vecF[i][j] += vecT[i][k] * vecT[k + 1][j] + vecF[i][k] * vecF[k + 1][j];
                    }
                }
            }
        }
        return vecT[0][len - 1];
    }
};
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值