#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 1;
const int AL = 26;
int n, match[N], pre[N][AL];
char answer[N];
string s;
void readInput(){
cin >> s;
n = s.size();
s = '*' + s;
}
void process(int l, int r){
if (l > r)
return;
int p = pre[r][s[l] - 'a'];
if (!p){
cout << -1;
exit(0);
}
answer[l] = '(';
answer[p] = ')';
process(l + 1, p - 1);
process(p + 1, r);
}
void solve(){
for(int i = 1; i <= n; i++)
for(int j = 0; j < AL; j++)
if (s[i] - 'a' == j)
pre[i][j] = i;
else
pre[i][j] = pre[max(0, pre[i - 1][s[i] - 'a'] - 1)][j];
process(1, n);
for(int i = 1; i <= n; i++)
cout << answer[i];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
readInput();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
2 ms |
1004 KB |
Output is correct |
9 |
Correct |
2 ms |
1132 KB |
Output is correct |
10 |
Correct |
2 ms |
1132 KB |
Output is correct |
11 |
Correct |
2 ms |
1260 KB |
Output is correct |
12 |
Correct |
11 ms |
7788 KB |
Output is correct |
13 |
Correct |
12 ms |
8428 KB |
Output is correct |
14 |
Correct |
13 ms |
9448 KB |
Output is correct |
15 |
Correct |
16 ms |
10912 KB |
Output is correct |
16 |
Correct |
15 ms |
10912 KB |
Output is correct |
17 |
Correct |
16 ms |
11496 KB |
Output is correct |
18 |
Correct |
16 ms |
10728 KB |
Output is correct |
19 |
Correct |
18 ms |
11752 KB |
Output is correct |
20 |
Correct |
11 ms |
8044 KB |
Output is correct |
21 |
Correct |
20 ms |
12648 KB |
Output is correct |