This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
string s, a;
int n, st[100000], sti;
vector<int> occ[26];
inline void fk() {
cout << -1;
exit(0);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s;
n=s.size();
for(int i=0; i<n; ++i)
occ[s[i]-'a'].push_back(i);
a.resize(n);
for(int i=0; i<26; ++i) {
if(occ[i].size()&1)
fk();
for(int j=0; j<(int)occ[i].size()-1-j; ++j)
a[occ[i][j]]='(', a[occ[i][occ[i].size()-1-j]]=')';
}
for(int i=0; i<n; ++i) {
if(a[i]==')'&&(!sti||s[st[--sti]]!=s[i]))
fk();
else if(a[i]=='(')
st[sti++]=i;
}
cout << a;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |