Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <stack>
- #include <string>
- #include <cstdio>
- using namespace std;
- int const MODUO = 1000000007;
- stack <char> operations; //stek gde se cuvaju operacije
- stack <pair <int, int> > numOfTrueFalse; //stek gde se cuvaju parovi (broj )
- vector <int> sol; //vektor gde se cuvaju resenja koja se kasnije ispisuju
- void doOperation(){
- char operation = operations.top();
- operations.pop();
- switch (operation){
- case 'a' :{
- pair <int, int> operand1 = numOfTrueFalse.top();
- numOfTrueFalse.pop();
- pair <int, int> operand2 = numOfTrueFalse.top();
- numOfTrueFalse.pop();
- int numOfTrue = (operand1.first * operand2.first)%MODUO;
- int numOfFalse = (((operand1.second * operand2.first)%MODUO + (operand1.first * operand2.second)%MODUO)%MODUO + (operand1.second * operand2.second)%MODUO)%MODUO;
- numOfTrueFalse.push(make_pair(numOfTrue, numOfFalse));
- break;
- }
- case 'o' :{
- pair <int, int> operand1 = numOfTrueFalse.top();
- numOfTrueFalse.pop();
- pair <int, int> operand2 = numOfTrueFalse.top();
- numOfTrueFalse.pop();
- int numOfFalse = (operand1.second * operand2.second)%MODUO;
- int numOfTrue = (((operand1.first * operand2.first)%MODUO + (operand1.first * operand2.second)%MODUO)%MODUO+ (operand1.second * operand2.first)%MODUO)%MODUO;
- numOfTrueFalse.push(make_pair(numOfTrue, numOfFalse));
- break;
- }
- case 'n' :{
- pair <int, int> operand = numOfTrueFalse.top();
- numOfTrueFalse.pop();
- numOfTrueFalse.push(make_pair(operand.second, operand.first));
- break;
- }
- default: break;
- }
- }
- void calculate(string str){
- for (int i = 0; str[i]!='\0'; i++){
- char c = tolower(str[i]);
- switch(c){
- case '(' : break;
- case 'x' : {
- numOfTrueFalse.push(make_pair(1,1));
- break;
- }
- case 'a' :{
- operations.push('a');
- i+=2;
- break;
- }
- case 'o' :{
- operations.push('o');
- i+=1;
- break;
- }
- case 'n' :{
- operations.push('n');
- i+=2;
- break;
- }
- case ')' : {
- doOperation();
- break;
- }
- case ',' : break;
- default: break;
- }
- }
- sol.push_back(numOfTrueFalse.top().first);
- numOfTrueFalse.pop();
- }
- int main()
- {
- int T;
- cin >> T;
- for (int i = 0; i<T; i++){
- string str;
- cin >> str;
- calculate(str);
- }
- for (int i=0; i<T; i++)
- cout << sol[i] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement