Với biểu thức boolean có chứa các ký hiệu {true, false, và, hoặc, xor}, đếm số cách để ngoặc đơn biểu thức sao cho nó đánh giá đúng.đếm boolean ngoặc đơn thực hiện
Ví dụ: chỉ có 1 cách để ngoặc đơn 'đúng và sai xor true' sao cho nó đánh giá đúng.
Đây là thuật toán của tôi
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.
Chúng ta có thể có một giải pháp tốt hơn?
Ý anh là gì với "Chúng ta có thể có một giải pháp tốt hơn"? Giả sử bạn đang yêu cầu một: Vui lòng xác định "tốt hơn". Bạn đang tìm kiếm giải pháp nhanh hơn, sử dụng ít mã hơn, ít sử dụng bộ nhớ hơn. Hơn nữa, bạn có thể muốn làm rõ mã giả của mình nhiều hơn một chút, tôi đã từng gặp khó khăn trong việc tìm ra cách chính xác. giúp đỡ nếu bạn viết những gì đang xảy ra bên trong switch – Grizzly
Tôi đang tìm kiếm giải pháp nhanh hơn và thực thi mã ít hơn. Ví dụ, nó là tẻ nhạt để tính toán cách ngoặc đơn, ngoặc đơn dẫn đến đúng và sai – SecureFish
Tôi đang bối rối - '(true và false) xor true' và 'true và (false xor true)' cả hai đều được đánh giá là true. –