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>
#define GOOD_LUCK ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define itn int
#define INF 100000000
#define MOD 998244353
#define MAX 103
#define endl "\n"
#define ff first
#define ss second
using namespace std;
int temp;
signed main() {
GOOD_LUCK
int t;
t = 1;
// cin >> t;
while (t--) {
int n;
string a;
cin >> a;
n = a.size();
if (n % 2 != 0) {
cout << -1;
return 0;
}
stack <char> s;
map <char, int> m;
for (int i=0; i < n; i++) {
m[a[i]]++;
if (s.empty()) s.push(a[i]);
else if (s.top() == a[i]) s.pop();
else s.push(a[i]);
}
/*for (auto i : m) {
cout << i.ff << ' ' << i.ss;
cout << endl;
}*/
if (s.empty()) {
for (auto i : m) {
if (i.ss % 2 != 0) {
cout << -1;
return 0;
}
}
string ans = "";
for (int i=0; i < n; i++) {
if (m[a[i]] > 0) {
ans = ans + '(';
m[a[i]]-=2;
}
else {
ans += ')';
}
}
cout << ans;
}
else {
cout << -1;
}
}
return 0;
}
// Problem C
// by Ekber_Ekber
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |