{true、false、and、or xor}の記号を含むブール式を指定すると、式をかっこにして真と評価する方法の数を数えます。ブール括弧の数を数える実装
たとえば、 '真と偽のxor true'を括弧でくくって真と評価する方法は1つしかありません。ここで
は私のアルゴリズム
we can calculate the total number of parenthesization of a string
Definition:
N - the total number of
True - the number of parenthesizations that evaluates to true
False - the number of parenthesizations that evaluates to false
True + False = N
Left_True - the number of parenthesization in the left part that evaluates to True
same to Left_False, Right_True, Right_False
we iterate the input string from left to right and deal with each operator as follows:
if it is "and", the number of parenthesization leads to true is
Left_True * Right_True;
if it is "xor", the number of parenthesization leads to true
Left_True * Right_False + Left_False * Right_True
if it is 'or', the number is
N - Left_False * Right_False
Here is my psuedocode
n = number of operator within the String
int[n][n] M; // save number of ways evaluate to true
for l = 2 to n
for i = 1 to n-l+1
do j = i+l-1
// here we have different string varying from 2 to n starting from i and ending at j
for k = i to j-1
// (i,k-1) is left part
// (k+1, j) is right part
switch(k){
case 'and': // calculate, update array m
case 'or': // same
case 'xor':
}
we save all the solutions to subproblems and read them when we meet them again. thus save time.
は、我々はより良いソリューションを持つことができますか?
あなたは、 "私たちはより良いソリューションを持つことができます" とは何を意味するのですか?あなたが1つを求めていると仮定します: "より良い"を定義してください。より少ないコードを使用し、より少ないメモリ使用量を必要とする、より高速なソリューションを探していますか?さらに、擬似コードをもう少し明確にしたいと思うかもしれません。スイッチの内部に何が入っているかを書いた場合に役立ちます。 – Grizzly
私は、より高速なソリューションと少ないコード実装を求めています。たとえば、かっこの方法を計算するのは面倒で、かっこは真と偽になります – SecureFish
私は混乱しています。 false)xor true'と 'trueとfalse(false xor true)'の両方がtrueに評価されます。 –