Pagini recente » Cod sursa (job #353325) | Cod sursa (job #2286492) | Istoria paginii runda/runda_de_test | Monitorul de evaluare | Cod sursa (job #1326448)
#include <algorithm>
#include <fstream>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
const int MAX_N = 1005;
const int AND = -1, OR = -2, NOT = -3;
int value[26];
int pr[MAX_N];
inline void correct(string &str) {
string c;
for (int i = 0; i < static_cast<int>(str.size());) {
if (str[i] == ' ') {
++ i;
continue;
}
if (str[i] == '(' || str[i] == ')') {
c += str[i];
++ i;
continue;
}
if (isalpha(str[i])) {
string n;
while (i < static_cast<int>(str.size()) && 'A' <= str[i] && str[i] <= 'Z') {
n += str[i];
++ i;
}
if (n == "TRUE") {
c += '1';
} else if (n == "FALSE") {
c += '0';
} else if (n == "AND") {
c += '&';
} else if (n == "OR") {
c += '|';
} else if (n == "NOT") {
c += '~';
} else {
c += n;
}
continue;
}
}
str = c;
}
string str;
int solve(int x, int y) {
if (str[x] == '(' && str[y] == ')' && pr[x] == y) {
return solve(x + 1, y - 1);
}
if (x == y) {
return value[str[x] - 'A'];
}
vector<int> values;
for (int i = x; i <= y; ++ i) {
if (str[i] == '(') {
values.push_back(solve(i + 1, pr[i] - 1));
i = pr[i];
} else if (str[i] == '&') {
values.push_back(AND);
} else if (str[i] == '|') {
values.push_back(OR);
} else if (str[i] == '~') {
if (!values.empty() && values.back() == NOT) {
values.pop_back();
} else {
values.push_back(NOT);
}
} else {
values.push_back(value[str[i] - 'A']);
}
}
int sum = 0, prod = 1;
for (int i = 0; i < values.size(); ++ i) {
if (values[i] == NOT) {
prod &= (values[i + 1] ^ 1);
++ i;
} else if (values[i] == AND) {
continue;
} else if (values[i] == OR) {
sum |= prod;
prod = 1;
} else {
prod &= values[i];
}
}
sum |= prod;
return sum;
}
int main() {
ifstream fin("bool.in");
ofstream fout("bool.out");
int n;
getline(fin, str);
correct(str);
stack<int> st;
for (int i = 0; i < static_cast<int>(str.size()); ++ i) {
if (str[i] == ')') {
pr[i] = st.top();
pr[st.top()] = i;
st.pop();
} else if (str[i] == '(') {
st.push(i);
}
}
cout << str << "\n";
fin >> n; fin.ignore();
for (int i = 1; i <= n; ++ i) {
char ch;
fin >> ch;
value[ch - 'A'] ^= 1;
fout << solve(0, str.size() - 1);
}
fout << "\n";
return 0;
}