Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. #include <string>
  5. #include <cstdio>
  6.  
  7. using namespace std;
  8.  
  9. int const MODUO = 1000000007;
  10. stack <char> operations; //stek gde se cuvaju operacije
  11. stack <pair <int, int> > numOfTrueFalse; //stek gde se cuvaju parovi (broj )
  12. vector <int> sol; //vektor gde se cuvaju resenja koja se kasnije ispisuju
  13.  
  14. void doOperation(){
  15. char operation = operations.top();
  16. operations.pop();
  17.  
  18. switch (operation){
  19. case 'a' :{
  20. pair <int, int> operand1 = numOfTrueFalse.top();
  21. numOfTrueFalse.pop();
  22. pair <int, int> operand2 = numOfTrueFalse.top();
  23. numOfTrueFalse.pop();
  24.  
  25. int numOfTrue = (operand1.first * operand2.first)%MODUO;
  26. int numOfFalse = (((operand1.second * operand2.first)%MODUO + (operand1.first * operand2.second)%MODUO)%MODUO + (operand1.second * operand2.second)%MODUO)%MODUO;
  27. numOfTrueFalse.push(make_pair(numOfTrue, numOfFalse));
  28. break;
  29. }
  30.  
  31. case 'o' :{
  32. pair <int, int> operand1 = numOfTrueFalse.top();
  33. numOfTrueFalse.pop();
  34. pair <int, int> operand2 = numOfTrueFalse.top();
  35. numOfTrueFalse.pop();
  36.  
  37. int numOfFalse = (operand1.second * operand2.second)%MODUO;
  38. int numOfTrue = (((operand1.first * operand2.first)%MODUO + (operand1.first * operand2.second)%MODUO)%MODUO+ (operand1.second * operand2.first)%MODUO)%MODUO;
  39. numOfTrueFalse.push(make_pair(numOfTrue, numOfFalse));
  40. break;
  41. }
  42.  
  43. case 'n' :{
  44. pair <int, int> operand = numOfTrueFalse.top();
  45. numOfTrueFalse.pop();
  46. numOfTrueFalse.push(make_pair(operand.second, operand.first));
  47. break;
  48. }
  49. default: break;
  50.  
  51. }
  52. }
  53.  
  54. void calculate(string str){
  55. for (int i = 0; str[i]!='\0'; i++){
  56. char c = tolower(str[i]);
  57. switch(c){
  58. case '(' : break;
  59. case 'x' : {
  60. numOfTrueFalse.push(make_pair(1,1));
  61. break;
  62. }
  63. case 'a' :{
  64. operations.push('a');
  65. i+=2;
  66. break;
  67. }
  68. case 'o' :{
  69. operations.push('o');
  70. i+=1;
  71. break;
  72. }
  73. case 'n' :{
  74. operations.push('n');
  75. i+=2;
  76. break;
  77. }
  78. case ')' : {
  79. doOperation();
  80. break;
  81. }
  82. case ',' : break;
  83. default: break;
  84. }
  85. }
  86. sol.push_back(numOfTrueFalse.top().first);
  87. numOfTrueFalse.pop();
  88. }
  89.  
  90.  
  91. int main()
  92. {
  93. int T;
  94. cin >> T;
  95. for (int i = 0; i<T; i++){
  96. string str;
  97. cin >> str;
  98. calculate(str);
  99. }
  100.  
  101. for (int i=0; i<T; i++)
  102. cout << sol[i] << endl;
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement