#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] = -1;
}
to[i][s[i] - 'a'] = i;
if (i == 0) continue;
int x = to[i - 1][s[i] - 'a'];
if (x <= 0) continue;
for (int j = 0; j < 26; j++) {
if (j == s[i] - 'a') continue;
to[i][j] = to[x - 1][j];
}
}
bool ok = solve(0, n - 1);
cout << (!ok ? "-1" : ans) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
return 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 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 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 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
976 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1096 KB |
Output is correct |
12 |
Correct |
7 ms |
7416 KB |
Output is correct |
13 |
Correct |
6 ms |
8152 KB |
Output is correct |
14 |
Correct |
7 ms |
9044 KB |
Output is correct |
15 |
Correct |
8 ms |
10328 KB |
Output is correct |
16 |
Correct |
8 ms |
10328 KB |
Output is correct |
17 |
Correct |
9 ms |
10964 KB |
Output is correct |
18 |
Correct |
9 ms |
10444 KB |
Output is correct |
19 |
Correct |
9 ms |
11508 KB |
Output is correct |
20 |
Correct |
6 ms |
7804 KB |
Output is correct |
21 |
Correct |
10 ms |
12400 KB |
Output is correct |