You are given a boolean expression consisting of a string of the symbols 'true', 'false', 'and', 'or', and 'xor'. Count the number of ways to parenthesize the expression such that it will evaluate to true. For example, there are 2 ways to parenthesize 'true and false xor true' such that it evaluates to true.
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.
Examples:
Input: symbol[] = {T, F, T} operator[] = {^, &} Output: 2 The given expression is "T ^ F & T", it evaluates true in two ways "((T ^ F) & T)" and "(T ^ (F & T))" Input: symbol[] = {T, F, F} operator[] = {^, |} Output: 2 The given expression is "T ^ F | F", it evaluates true in two ways "( (T ^ F) | F )" and "( T ^ (F | F) )". Input: symbol[] = {T, T, F, T} operator[] = {|, &, ^} Output: 4 The given expression is "T | T & F ^ T", it evaluates true in 4 ways ((T|T)&(F^T)), (T|(T&(F^T))), (((T|T)&F)^T) and (T|((T&F)^T)).
Solution:
Let T(i, j) represents the number of ways to parenthesize the symbols between i and j (both inclusive) such that the subexpression between i and j evaluates to true.
Let F(i, j) represents the number of ways to parenthesize the symbols between i and j (both inclusive) such that the subexpression between i and j evaluates to false.
Base Cases:
T(i, i) = 1 if symbol[i] = 'T' T(i, i) = 0 if symbol[i] = 'F' F(i, i) = 1 if symbol[i] = 'F' F(i, i) = 0 if symbol[i] = 'T'
The cost is :
checking subwords of size 1 + checking subwords of size 2 + ... + checking subwords of size N
= N + N-1 + 2*(N-2) + 2*(N-2) + .. + (N-1)*(1)
= O(N^3)
Auxiliary Space: O(n2)
public int countBoolParenth(char[] symbols, char[] operators) { int n = symbols.length; int[][] T = new int[n][n]; int[][] F = new int[n][n]; for (int i = 0; i < n; i++) { T[i][i] = symbols[i] == 'T' ? 1 : 0; F[i][i] = symbols[i] == 'F' ? 1 : 0; } for (int w = 2; w <= n; w++) { // w: the width of the sliding window, [2, n] for (int i = 0, j = w-1; j < n; i++, j++) { // i..j: the sliding window, [i, j] for (int k = i; k < j; k++) { // k: the index of operator to be inserted into symbols // Store Total[i][k] and Total[k+1][j] int tik = T[i][k] + F[i][k]; int tkj = T[k + 1][j] + F[k + 1][j]; // Follow the recursive formulas according to the current operator if (operators[k] == '&') { T[i][j] += T[i][k] * T[k + 1][j]; F[i][j] += (tik * tkj - T[i][k] * T[k + 1][j]); } if (operators[k] == '|') { F[i][j] += F[i][k] * F[k + 1][j]; T[i][j] += (tik * tkj - F[i][k] * F[k + 1][j]); } if (operators[k] == '^') { T[i][j] += F[i][k] * T[k + 1][j] + T[i][k] * F[k + 1][j]; F[i][j] += T[i][k] * T[k + 1][j] + F[i][k] * F[k + 1][j]; } } } } return T[0][n - 1]; }
References:
http://www.geeksforgeeks.org/dynamic-programming-set-37-boolean-parenthesization-problem/
http://people.cs.clemson.edu/~bcdean/dp_practice/dp_9.swf
http://ineptprogrammer.blogspot.com/2014/11/counting-boolean-parenthesizations-you.html
http://courses.csail.mit.edu/6.006/fall10/handouts/dpproblems-sol.pdf
相关推荐
- counting boolean parenthesizations - edit distance - integer knapsack - longest increasing subsequences - making changes - maximum value contiguous subsequences - optimal strategy for a game
"Source统计工具Counting"是一款专门用于分析编程源代码的实用工具,它的主要功能是统计源代码中的有效行、空行以及注释行的数量。在软件开发和维护过程中,这样的统计信息对于理解代码规模、复杂度和维护性具有重要...
《3D人员计数实施指南》是针对使用毫米波雷达技术进行三维人体计数的软件实现文档,由德州仪器(Texas Instruments)发布。本指南详细介绍了如何设置演示系统,以及利用SDK组件进行3D人员计数的具体步骤。...
These practices are a compilation of acceptable proce- dures for cycle-counting methods employed in fatigue analysis. This standard does not intend to recommend a particular method.
在IT领域,分形盒维数(Box-Counting Dimension)是研究复杂几何形状和结构的一种重要工具,尤其在图像处理、数据建模和复杂网络分析中有着广泛的应用。本项目"53958727box-counting.zip"显然是针对1D、2D、3D空间中...
《代码行统计工具Counting.exe详解》 在软件开发过程中,了解代码的规模是至关重要的。这不仅可以评估项目的复杂性,还可以为项目管理和资源分配提供参考。Counting.exe是一款高效实用的代码行统计工具,它支持多种...
The Pleasures of Counting 1996 © Cambridge University Press 1996
标题中的“counting 代码行数统计”指的是在软件开发过程中对源代码文件中的代码行进行计数的活动。这通常用于评估项目的工作量、跟踪开发进度或比较不同版本的代码变化。代码行数统计可以帮助开发者理解代码的复杂...
Function Point Counting Practices Manual 4.1.1 版本。 来自IFPUG,权威资料 The primary objectives of the IFPUG Counting Practices Manual, Release 4.1, are to • Provide a clear and detailed description...
Box Counting方法是一种在复杂系统和分形几何中计算维度的数学技术,它主要用于分析和理解数据集或图像的自相似性。这个压缩包“box_count.zip”包含了用于执行Box Counting算法的MATLAB代码,这有助于计算一个系统...
1004. Counting Leaves (30) 来自:http://blog.csdn.net/sunbaigui/article/details/8657008
Counting源代码统计器,对开发的代码进行统计,辅助开发者进行开发,也可以用于软件测试时对代码的统计
差分盒计数法(Differential Box Counting,简称DBC)是一种通过统计不同大小的“盒子”覆盖图像中点的数量来估计分形维数的方法。分形维数是一个无量纲的数值,它描述了对象在不同尺度下的复杂性。在图像处理中,这...
论文Attentional Neural Fields for Crowd Counting,侵删
《功能点计算实践手册》V4.3是软件工程领域中一种重要的估算工具,主要用于量化软件项目的规模,以便更准确地预测开发成本、时间和资源需求。功能点分析(Function Point Analysis,FPA)是一种非度量的技术,它通过...
在众多应用中,跨场景人群计数(Cross-Scene Crowd Counting)技术尤其受到关注。这项技术能够自动统计不同场景下的人群数量,为城市管理提供数据支持,对于预防拥挤事故、优化人流疏导策略具有重要意义。 #### 二...
"simple_vehicle_counting-master"项目就是一个很好的实例,它利用先进的算法和技术实现对车辆的自动识别与计数。 车辆检测与计数的主要目标是实时监控交通流量,为城市交通管理、道路安全评估以及交通规划提供数据...
To reflect that, and to make the Counting Practices Manual (CPM) even more attractive as a reference manual, the Counting Practices Committee (CPC) decided to restructure CPM 4.2 into four parts: ...
统计代码量的工具 软件开发必备 简单易操作