Advertisement
Malinovsky239

Zksh Round2 (E)

Jan 22nd, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     freopen("brackets.in", "r", stdin);
  9.     freopen("brackets.out", "w", stdout);  
  10.  
  11.     string s;
  12.     cin >> s;
  13.  
  14.     int l = s.size();
  15.  
  16.     int open = 0;
  17.     for (int i = 0; i < l; i++)
  18.         if (s[i] == '(')
  19.             open++;
  20.  
  21.     int bal = 0;
  22.     for (int i = 0; i < l; i++) {
  23.         if (s[i] == '(')
  24.             bal++;
  25.         if (s[i] == ')')
  26.             bal--;
  27.         if (s[i] == '?') {
  28.             if (open < l / 2) {
  29.                 open++;
  30.                 bal++;
  31.                 s[i] = '(';
  32.             }
  33.             else {
  34.                 bal--;
  35.                 s[i] = ')';
  36.             }
  37.         }
  38.         if (bal < 0) {
  39.             cout << "No\n";
  40.             return 0;          
  41.         }
  42.     }
  43.  
  44.     if (open != l / 2 || bal) {
  45.         cout << "No\n";
  46.         return 0;
  47.     }
  48.  
  49.     cout << s << endl;
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement