1 solutions
-
0
C++ :
#include <bits/stdc++.h> using namespace std; char s[1000100], a[1000100]; stack<char> op; stack<int> number; int cnt, ans, ans1, ans2; int G[1000100][2]; void work(){ a[cnt] = op.top(); op.pop(); G[cnt][1] = number.top(); number.pop(); G[cnt][0] = number.top(); number.pop(); number.push(cnt++); } int dfs(int now){ if (a[now] == '1' || a[now] == '0'){ return a[now] - '0'; } if (a[now] == '&'){ if (dfs(G[now][0]) == 0){ ans1++; return 0; } return dfs(G[now][1]); } if (a[now] == '|'){ if (dfs(G[now][0]) == 1){ ans2++; return 1; } return dfs(G[now][1]); } } int main(){ scanf("%s", s); for (int i = 0; s[i] != '\0'; ++i){ if (s[i] == '('){ op.push('('); } else if (s[i] == ')'){ while (op.top() != '('){ work(); } op.pop(); } else if (s[i] == '&'){ while (op.size() && op.top() == '&'){ work(); } op.push('&'); } else if (s[i] == '|'){ while (op.size() && op.top() != '('){ work(); } op.push('|'); } else { a[cnt] = s[i]; number.push(cnt++); } } while (op.size()){ work(); } ans = dfs(cnt - 1); printf("%d\n%d %d\n", ans, ans1, ans2); return 0; }
- 1
Information
- ID
- 1186
- Time
- 1000ms
- Memory
- 512MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By