#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pl = pair<ll,ll>;
using pii = pair<int,int>;
using tpl = tuple<int,int,int>;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 1e5 + 5;
char bracket[mn];
string s; int n;
bool tryMatch (int k) {
stack<int> st;
for (int i = 1; i < k; i++) {
if (bracket[i] == '(') st.push(s[i]);
else {
if (st.empty() || st.top() != s[i]) {
cout << "Wrong";
exit(0);
}
st.pop();
}
}
for (int i = k; i <= n; i++) {
if (st.size() && st.top() == s[i]) st.pop();
else st.push(s[i]);
}
return st.empty();
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> s;
n = s.size();
s = " " + s;
if (!tryMatch(1)) return cout << -1, 0;
for (int i = 1; i <= n; i++) {
bracket[i] = '(';
if (!tryMatch(i + 1)) bracket[i] = ')';
}
for (int i = 1; i <= n; i++) cout << bracket[i];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |