#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, int> ri3;
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) begin(x), end(x)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)
const int MAXN = 1e5+5;
string S;
int N;
int prevStart[MAXN][26];
stack<int> stk;
string solve(int a, int b) {
if (a > b) return "";
if (a+1 == b) return "()";
int idx = prevStart[b][S[a]-'a'];
return '(' + solve(a+1,idx-1) + ')' + solve(idx+1,b);
}
int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
cin >> S;
N = S.length();
memset(prevStart, -1, sizeof prevStart);
FOR(i,0,N-1){
FOR(j,0,25){
if (j == S[i]-'a') prevStart[i][j] = i;
else {
if (i == 0 or prevStart[i-1][S[i]-'a'] <= 0) prevStart[i][j] = -1;
else prevStart[i][j] = prevStart[prevStart[i-1][S[i]-'a']-1][j];
}
}
}
FOR(i,0,N-1){
if (!stk.empty() and S[stk.top()] == S[i]) stk.pop();
else stk.push(i);
}
if (!stk.empty()) cout << -1 << '\n';
else cout << solve(0,N-1) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
10488 KB |
Output is correct |
2 |
Correct |
10 ms |
10488 KB |
Output is correct |
3 |
Correct |
10 ms |
10488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
10488 KB |
Output is correct |
2 |
Correct |
10 ms |
10488 KB |
Output is correct |
3 |
Correct |
10 ms |
10488 KB |
Output is correct |
4 |
Correct |
10 ms |
10616 KB |
Output is correct |
5 |
Correct |
10 ms |
10488 KB |
Output is correct |
6 |
Correct |
10 ms |
10616 KB |
Output is correct |
7 |
Correct |
11 ms |
10616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
10488 KB |
Output is correct |
2 |
Correct |
10 ms |
10488 KB |
Output is correct |
3 |
Correct |
10 ms |
10488 KB |
Output is correct |
4 |
Correct |
10 ms |
10616 KB |
Output is correct |
5 |
Correct |
10 ms |
10488 KB |
Output is correct |
6 |
Correct |
10 ms |
10616 KB |
Output is correct |
7 |
Correct |
11 ms |
10616 KB |
Output is correct |
8 |
Correct |
11 ms |
10616 KB |
Output is correct |
9 |
Correct |
12 ms |
10872 KB |
Output is correct |
10 |
Correct |
11 ms |
10744 KB |
Output is correct |
11 |
Correct |
12 ms |
11000 KB |
Output is correct |
12 |
Correct |
30 ms |
13688 KB |
Output is correct |
13 |
Correct |
34 ms |
14072 KB |
Output is correct |
14 |
Correct |
53 ms |
15480 KB |
Output is correct |
15 |
Correct |
87 ms |
16376 KB |
Output is correct |
16 |
Correct |
87 ms |
16376 KB |
Output is correct |
17 |
Correct |
69 ms |
16760 KB |
Output is correct |
18 |
Correct |
65 ms |
13304 KB |
Output is correct |
19 |
Correct |
58 ms |
15128 KB |
Output is correct |
20 |
Correct |
42 ms |
14712 KB |
Output is correct |
21 |
Correct |
89 ms |
16620 KB |
Output is correct |