This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using vi = V<int>;
using ll = long long;
using pl = pair<ll, ll>;
using vl = V<ll>;
int main() {
cin.tie(0)->sync_with_stdio(0);
string S; cin >> S;
int N = sz(S);
string ans(N, '-');
V<map<int, vector<int>>> oc(26);
vi P = {0};
for(int i = 0; i < N; i++) {
int c = S[i] - 'a'; P.pb(P.back() ^ (1 << c));
// cout << i << " " << S[i] << " " << P.back() << endl;
oc[c][P.back()].pb(i);
}
function<void(int, int)> dnc = [&](int l, int r) {
if (l > r) return;
int lc = S[l] - 'a';
// cout << l << " " << r << endl;
auto it = upper_bound(begin(oc[lc][P[l]]), end(oc[lc][P[l]]), r);
if (it == begin(oc[lc][P[l]])) {
cout << -1 << nl;
exit(0-0);
}
int m = *prev(it);
if (l > m) {
cout << -1 << nl;
exit(0-0);
}
ans[l] = '(', ans[m] = ')';
dnc(l + 1, m - 1); dnc(m + 1, r);
};
dnc(0, N - 1);
cout << ans << nl;
exit(0-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... |