Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define times(I, X) for (int I=0; I<X; I++)
  4.  
  5. #ifdef DEBUG
  6.     #define deb(X) X
  7.     #define log(X) std::cout << X << "\n"
  8.     #define dump(X) std::cout << __LINE__ << "L: [" << #X << "] = [" << X << "]\n"
  9. #else
  10.     #define deb(X)
  11.     #define log(X)
  12.     #define dump(X)
  13. #endif
  14. #define fi first
  15. #define se second
  16. #define pb push_back
  17.  
  18. using namespace std;
  19. using ll = long long;
  20. using ull = unsigned long long;
  21.  
  22.  
  23. int main() {
  24.     #ifndef DEBUG
  25.     ios_base::sync_with_stdio(0);
  26.     cin.tie(0);
  27.     cout.tie(0);
  28.     // freopen("forest.in", "r", stdin);
  29.     // freopen(s"forest.out", "w", stdout);
  30.     #endif
  31.     int n;
  32.     cin >> n;
  33.     string s;
  34.     cin >> s;
  35.     int r = n / 2, l = n / 2;
  36.     for(int i = 0; i < n; i++)
  37.     {
  38.         char c = s[i];
  39.         if(c == '(')
  40.         {
  41.             r--;
  42.         }
  43.         else if(c == ')')
  44.         {
  45.             l--;
  46.         }
  47.     }
  48.  
  49.     if(n % 2 == 1 || s[0] == ')' || s[n - 1] == '(')
  50.     {
  51.         cout << ":(";
  52.         return 0;
  53.     }
  54.  
  55.     int bracket = 0;
  56.  
  57.     for(int i = 0; i < n; i++)
  58.     {
  59.         if(s[i] == '(')
  60.         {
  61.             bracket++;
  62.         }
  63.         else if(s[i] == ')')
  64.         {
  65.             if(bracket == 0)
  66.             {
  67.                 cout << ":(";
  68.                 return 0;
  69.             }
  70.             else{
  71.                 bracket--;
  72.             }
  73.         }
  74.         else{
  75.             if(r > 0)
  76.             {
  77.                 s[i] = '(';
  78.                 r--;
  79.                 bracket++;
  80.             }
  81.             else{
  82.                 s[i] = ')';
  83.                 l--;
  84.                 if(bracket == 0)
  85.                 {
  86.                     cout << ":(";
  87.                     return 0;
  88.                 }
  89.                 else{
  90.                         bracket--;
  91.                 }
  92.             }
  93.         }
  94.         if(i != n - 1 && bracket == 0) {
  95.             cout << ":(";
  96.             return 0;
  97.         }
  98.     }
  99.     if(bracket != 0)
  100.     {
  101.         cout << ":(";
  102.         return 0;
  103.     }
  104.     cout << s;
  105.     return 0;
  106. }
  107.  
  108. //((?()(?)?)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement