// 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<unordered_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;
vi &OC = oc[lc][P[l]];
auto it = upper_bound(begin(OC), end(OC), r);
if (it == begin(OC)) {
cout << -1 << nl;
exit(0-0);
}
int m = *prev(it);
assert(l < m && m <= r);
ans[l] = '(';
ans[m] = ')';
dnc(l + 1, m - 1);
dnc(m + 1, r);
};
dnc(0, N - 1);
cout << ans << nl;
exit(0-0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
5 |
Halted |
0 ms |
0 KB |
- |