#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int maxn = 100001, k = 26;
int n;
string s;
int it[maxn], l[maxn], p[maxn];
int tr[maxn][k];
vector<int> v[maxn];
int m = 1;
string ret;
void solve(int l, int r){
if(l > r) return;
int x = *--upper_bound(v[it[l]].begin(), v[it[l]].end(), r);
ret[l - 1] = '(', ret[x] = ')';
solve(l + 1, x), solve(x + 2, r);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> s;
n = s.size();
l[0] = -1;
for(int i = 1; i <= n; i++){
int c = s[i - 1] - 'a';
if(l[it[i - 1]] == c){
it[i] = p[it[i - 1]];
}else{
if(!tr[it[i - 1]][c]) tr[it[i - 1]][c] = m++;
p[it[i] = tr[it[i - 1]][c]] = it[i - 1];
l[it[i]] = c;
}
v[it[i]].push_back(i);
}
if(it[n]){
cout << -1 << endl;
}else{
ret.resize(n);
solve(1, n);
cout << ret << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2816 KB |
Output is correct |
5 |
Correct |
6 ms |
2816 KB |
Output is correct |
6 |
Correct |
6 ms |
2816 KB |
Output is correct |
7 |
Correct |
6 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2816 KB |
Output is correct |
5 |
Correct |
6 ms |
2816 KB |
Output is correct |
6 |
Correct |
6 ms |
2816 KB |
Output is correct |
7 |
Correct |
6 ms |
2816 KB |
Output is correct |
8 |
Correct |
7 ms |
3072 KB |
Output is correct |
9 |
Correct |
7 ms |
3200 KB |
Output is correct |
10 |
Correct |
6 ms |
3328 KB |
Output is correct |
11 |
Correct |
8 ms |
3200 KB |
Output is correct |
12 |
Correct |
12 ms |
7680 KB |
Output is correct |
13 |
Correct |
12 ms |
8192 KB |
Output is correct |
14 |
Correct |
14 ms |
8576 KB |
Output is correct |
15 |
Correct |
11 ms |
4864 KB |
Output is correct |
16 |
Correct |
12 ms |
4864 KB |
Output is correct |
17 |
Correct |
16 ms |
7552 KB |
Output is correct |
18 |
Correct |
12 ms |
4352 KB |
Output is correct |
19 |
Correct |
17 ms |
9984 KB |
Output is correct |
20 |
Correct |
13 ms |
7808 KB |
Output is correct |
21 |
Correct |
19 ms |
9984 KB |
Output is correct |