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>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 1e5 + 5;
int to[mxN][26];
string ans;
string s;
bool solve(int l, int r) {
if (l > r) return true;
if (l == r) return false;
if (to[r][s[l] - 'a'] <= l) return false;
ans[l] = '(', ans[to[r][s[l] - 'a']] = ')';
bool ok = solve(l + 1, to[r][s[l] - 'a'] - 1);
ok &= solve(to[r][s[l] - 'a'] + 1, r);
return ok;
}
void testCase() {
cin >> s;
int n = s.size();
ans = string(n, 'a');
for (int i = 0; i < n; i++) {
for (int j = 0; j < 26; j++) {
to[i][j] = (i == 0 ? -1 : to[i - 1][j]);
}
to[i][s[i] - 'a'] = i;
}
bool ok = solve(0, n - 1);
cout << (!ok ? "-1" : ans) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
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... |