Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("balans2.in");
- ofstream fout("balans2.out");
- int main() {
- fin.sync_with_stdio(false);
- fout.sync_with_stdio(false);
- fin.tie(nullptr);
- fout.tie(nullptr);
- int N;
- string s;
- fin >> N >> s;
- bool found = false;
- for(int i = 0; i < N && !found; ++i)
- if(s[i] == 'P')
- found = true;
- if(!found) {
- bool wrong = false;
- int cnt = 0;
- for(int i = 0; i < N; ++i) {
- if(s[i] == '(')
- ++cnt;
- else
- --cnt;
- if(cnt == 0 && !wrong)
- fout << '1';
- else {
- fout << '0';
- if(cnt < 0)
- wrong = true;
- }
- }
- return 0;
- }
- int cnt = 0;
- queue < int > Q, last;
- char correct = '1';
- for(int i = 0; i < N; ++i) {
- if(s[i] == '(' || s[i] == ')') {
- if(s[i] == '(')
- ++cnt;
- else
- --cnt;
- if(cnt != 0) {
- correct = '0';
- if(cnt < 0 && s[i] == ')') {
- last.emplace(i);
- cnt = 0;
- }
- }
- else {
- if(last.empty())
- correct = '1';
- else
- correct = '0';
- }
- Q.emplace(i);
- }
- else {
- int curr = Q.front();
- Q.pop();
- if(!last.empty() && last.front() == curr)
- last.pop();
- else {
- if(s[curr] == '(')
- --cnt;
- else
- ++cnt;
- }
- if(correct == '1')
- correct = '0';
- else {
- if(last.empty() && cnt == 0)
- correct = '1';
- else
- correct = '0';
- }
- }
- fout << correct;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment