#include <bits/stdc++.h>
using namespace std;
const int mxN=1e5;
string s, a;
int n, st[mxN+1], c[mxN+1][26], p[mxN+1], ts=1;
vector<int> occ[mxN+1];
inline int gc(int i, int j) {
if(!c[i][j]) {
c[i][j]=ts++;
p[c[i][j]]=i;
}
return c[i][j];
}
void rec(int l, int r) {
if(l>=r)
return;
int m=*--upper_bound(occ[st[l+1]].begin(), occ[st[l+1]].end(), r-1);
a[l]='(';
a[m]=')';
rec(l+1, m);
rec(m+1, r);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s;
n=s.size();
occ[0].push_back(0);
for(int i=0, j=0; i<n; ++i) {
if(j&&s[occ[p[j]].back()]==s[i])
j=p[j];
else
j=gc(j, s[i]-'a');
st[i+1]=j;
occ[j].push_back(i+1);
}
if(occ[0].back()!=n) {
cout << -1;
return 0;
}
a.resize(n);
rec(0, n);
cout << a;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2648 KB |
Output is correct |
2 |
Correct |
5 ms |
2840 KB |
Output is correct |
3 |
Correct |
4 ms |
2840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2648 KB |
Output is correct |
2 |
Correct |
5 ms |
2840 KB |
Output is correct |
3 |
Correct |
4 ms |
2840 KB |
Output is correct |
4 |
Correct |
6 ms |
2904 KB |
Output is correct |
5 |
Correct |
6 ms |
2952 KB |
Output is correct |
6 |
Correct |
6 ms |
3016 KB |
Output is correct |
7 |
Correct |
6 ms |
3256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2648 KB |
Output is correct |
2 |
Correct |
5 ms |
2840 KB |
Output is correct |
3 |
Correct |
4 ms |
2840 KB |
Output is correct |
4 |
Correct |
6 ms |
2904 KB |
Output is correct |
5 |
Correct |
6 ms |
2952 KB |
Output is correct |
6 |
Correct |
6 ms |
3016 KB |
Output is correct |
7 |
Correct |
6 ms |
3256 KB |
Output is correct |
8 |
Correct |
6 ms |
3420 KB |
Output is correct |
9 |
Correct |
7 ms |
3572 KB |
Output is correct |
10 |
Correct |
9 ms |
3660 KB |
Output is correct |
11 |
Correct |
7 ms |
3712 KB |
Output is correct |
12 |
Correct |
16 ms |
7980 KB |
Output is correct |
13 |
Correct |
18 ms |
8428 KB |
Output is correct |
14 |
Correct |
19 ms |
9000 KB |
Output is correct |
15 |
Correct |
13 ms |
9000 KB |
Output is correct |
16 |
Correct |
14 ms |
9000 KB |
Output is correct |
17 |
Correct |
20 ms |
9000 KB |
Output is correct |
18 |
Correct |
12 ms |
9000 KB |
Output is correct |
19 |
Correct |
30 ms |
10736 KB |
Output is correct |
20 |
Correct |
22 ms |
10736 KB |
Output is correct |
21 |
Correct |
25 ms |
10984 KB |
Output is correct |