#include <bits/stdc++.h>
using namespace std;
int n; string s; string ans;
int dp[100005][26];
bool solve(int l, int r){
if(l == r+1) return 1;
if(dp[r][s[l]-'a'] < l) return false;
int a = dp[r][s[l]-'a'];
ans[l] = '(', ans[a-1] = ')';
return solve(l+1, a-2) && solve(a, r);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> s; n = s.length(); ans.resize(n);
for(int i = 0; i<n; i++){
for(int j = 0; j<26; j++){
dp[i][j] = -1;
if(s[i]-'a' == j) dp[i][j] = i+1;
if(i >= 1 && dp[i-1][s[i]-'a']-2 >= 0) dp[i][j] = max(dp[i][j], dp[dp[i-1][s[i]-'a']-2][j]);
}
}
if(!solve(0, n-1)) cout << -1 << endl;
else cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
492 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 |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
1024 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 |
10 ms |
7404 KB |
Output is correct |
13 |
Correct |
11 ms |
8044 KB |
Output is correct |
14 |
Correct |
12 ms |
8812 KB |
Output is correct |
15 |
Correct |
16 ms |
10220 KB |
Output is correct |
16 |
Correct |
15 ms |
10220 KB |
Output is correct |
17 |
Correct |
15 ms |
10732 KB |
Output is correct |
18 |
Correct |
17 ms |
10476 KB |
Output is correct |
19 |
Correct |
17 ms |
11244 KB |
Output is correct |
20 |
Correct |
11 ms |
7532 KB |
Output is correct |
21 |
Correct |
18 ms |
12012 KB |
Output is correct |