#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
int pre[MX][30];
string s, ans; int n;
void solve(int l, int r){
if(l > r) return;
if(pre[r][s[l] - 'a'] <= l){
cout << -1 << endl;
exit(0);
}
ans[l] = '(';
ans[pre[r][s[l] - 'a']] = ')';
solve(l + 1, pre[r][s[l] - 'a'] - 1);
solve(pre[r][s[l] - 'a'] + 1, r);
}
int main(){
cin >> s;
n = s.size(); ans = "";
for(int i = 0; i < n; i ++){
ans += '(';
for(int j = 0; j < 26; j ++){
if(j == s[i] - 'a') pre[i][j] = i;
else if(i > 0 && pre[i - 1][s[i] - 'a'] > 0)
pre[i][j] = pre[pre[i - 1][s[i] - 'a'] - 1][j];
else pre[i][j] = -1;
}
}
solve(0, n - 1);
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
308 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 |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
308 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 |
1076 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1236 KB |
Output is correct |
12 |
Correct |
12 ms |
8308 KB |
Output is correct |
13 |
Correct |
9 ms |
9044 KB |
Output is correct |
14 |
Correct |
11 ms |
9940 KB |
Output is correct |
15 |
Correct |
12 ms |
11604 KB |
Output is correct |
16 |
Correct |
16 ms |
11656 KB |
Output is correct |
17 |
Correct |
12 ms |
12244 KB |
Output is correct |
18 |
Correct |
18 ms |
11932 KB |
Output is correct |
19 |
Correct |
16 ms |
12756 KB |
Output is correct |
20 |
Correct |
9 ms |
8512 KB |
Output is correct |
21 |
Correct |
17 ms |
13600 KB |
Output is correct |