#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
int n; string s;
int dp[MAXN];
string ans;
bool solve(int l, int r){
if(l == r+1) return 1;
if(dp[l] == -1) return false;
ans[l] = '('; ans[dp[l]] = ')';
return solve(l+1, dp[l]-1) && solve(dp[l]+1, r);
}
int main(){
// freopen("match.in", "r", stdin);
// freopen("match.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> s; n = s.length();
ans.resize(n);
dp[n] = -1;
for(int i = n-1; i>=0; i--){
dp[i] = -1;
if(i != n-1 && s[i] == s[i+1]) dp[i] = i+1;
int cur = i+1;
while(dp[cur] != -1){
cur = dp[cur]+1;
if(cur != n && s[cur] == s[i]) dp[i] = cur;
}
}
if(!solve(0, n-1)) cout << -1 << endl;
else cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |