fork(5) download
  1. import java.util.*;
  2.  
  3. class Ideone {
  4.  
  5. public static void main(String[] args){
  6. char[] symbols = {'T', 'T', 'F', 'T'};
  7. char[] operators = {'|', '&', '^'};
  8.  
  9. int n = symbols.length;
  10. int[][] F = new int[n][n];
  11. int[][] T = new int[n][n];
  12.  
  13. for(int i=0; i<n; i++){
  14. F[i][i] = symbols[i] == 'F' ? 1 : 0;
  15. T[i][i] = symbols[i] == 'T' ? 1 : 0;
  16. }
  17.  
  18. for(int gap=1; gap<n; gap++){
  19. for(int i=0, j=gap; j<n; j++, i++){
  20. T[i][j] = 0;
  21. F[i][j] = 0;
  22.  
  23. for(int g=0; g<gap;g++){
  24. // Find place of parenthesization using current value of gap
  25. int k = i + g;
  26.  
  27. // Store Total[i][k] and Total[k+1][j]
  28. int tik = T[i][k] + F[i][k];
  29. int tkj = T[k+1][j] + F[k+1][j];
  30.  
  31. // Follow the recursive formulas according to the current operator
  32. if (operators[k] == '&'){
  33. T[i][j] += T[i][k]*T[k+1][j];
  34. F[i][j] += (tik*tkj - T[i][k]*T[k+1][j]);
  35. }
  36.  
  37. if (operators[k] == '|'){
  38. F[i][j] += F[i][k]*F[k+1][j];
  39. T[i][j] += (tik*tkj - F[i][k]*F[k+1][j]);
  40. }
  41.  
  42. if (operators[k] == '^'){
  43. T[i][j] += F[i][k]*T[k+1][j] + T[i][k]*F[k+1][j];
  44. F[i][j] += T[i][k]*T[k+1][j] + F[i][k]*F[k+1][j];
  45. }
  46. }
  47. }
  48. }
  49. System.out.println("Number of paranthesis => "+ T[0][n-1]);
  50. }
  51. }
  52.  
Success #stdin #stdout 0.06s 380160KB
stdin
Standard input is empty
stdout
Number of paranthesis => 4