Alex_tz307

Balans infopro XI

Nov 14th, 2020 (edited)
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1.    
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("balans2.in");
  7. ofstream fout("balans2.out");
  8.  
  9. int main() {
  10.     fin.sync_with_stdio(false);
  11.     fout.sync_with_stdio(false);
  12.     fin.tie(nullptr);
  13.     fout.tie(nullptr);
  14.     int N;
  15.     string s;
  16.     fin >> N >> s;
  17.     bool found = false;
  18.     for(int i = 0; i < N && !found; ++i)
  19.         if(s[i] == 'P')
  20.             found = true;
  21.     if(!found) {
  22.         bool wrong = false;
  23.         int cnt = 0;
  24.         for(int i = 0; i < N; ++i) {
  25.             if(s[i] == '(')
  26.                 ++cnt;
  27.             else
  28.                 --cnt;
  29.             if(cnt == 0 && !wrong)
  30.                 fout << '1';
  31.             else {
  32.                 fout << '0';
  33.                 if(cnt < 0)
  34.                     wrong = true;
  35.             }
  36.         }
  37.         return 0;
  38.     }
  39.     int cnt = 0;
  40.     queue < int > Q, last;
  41.     char correct = '1';
  42.     for(int i = 0; i < N; ++i) {
  43.         if(s[i] == '(' || s[i] == ')') {
  44.             if(s[i] == '(')
  45.                 ++cnt;
  46.             else
  47.                 --cnt;
  48.             if(cnt != 0) {
  49.                 correct = '0';
  50.                 if(cnt < 0 && s[i] == ')') {
  51.                     last.emplace(i);
  52.                     cnt = 0;
  53.                 }
  54.             }
  55.             else {
  56.                 if(last.empty())
  57.                     correct = '1';
  58.                 else
  59.                     correct = '0';
  60.             }
  61.             Q.emplace(i);
  62.         }
  63.         else {
  64.             int curr = Q.front();
  65.             Q.pop();
  66.             if(!last.empty() && last.front() == curr)
  67.                 last.pop();
  68.             else {
  69.                 if(s[curr] == '(')
  70.                     --cnt;
  71.                 else
  72.                     ++cnt;
  73.             }
  74.             if(correct == '1')
  75.                 correct = '0';
  76.             else {
  77.                 if(last.empty() && cnt == 0)
  78.                     correct = '1';
  79.                 else
  80.                     correct = '0';
  81.             }
  82.         }
  83.         fout << correct;
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment