This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Author RufatM
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 1e9+7
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define vii vector<vector<int>>
#define mii map<int,int>
#define pb push_back
#define pii pair<ll,int>
#define all(a) a.begin(),a.end()
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
signed main(){
int t=1;
//cin >> t;
while(t--){
fastio;
string S;
cin >> S;
int N = S.length();
vector<char> B(N);
stack<pair<char,int>> st;
for(int i = 0; i < N; i++) {
char c = S[i];
if(!st.empty() && st.top().first == c) {
B[st.top().second] = '(';
B[i] = ')';
st.pop();
} else {
st.push({c, i});
}
}
if(!st.empty()) {
cout << "-1\n";
return 0;
}
int balance = 0;
for(int i = 0; i < N; i++) {
if(B[i] == '(') balance++;
else if(B[i] == ')') balance--;
else {
cout << "-1\n";
return 0;
}
if(balance < 0) {
cout << "-1\n";
return 0;
}
}
if(balance != 0) {
cout << "-1\n";
return 0;
}
for(int i = 0; i < N; i++) {
cout << B[i];
}
cout << '\n';
}
}
//Allahim insallah tle vermez
//Yasasin Yaxsi Kiwiler
//Yasasin Info Azerbaijan
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |