#include <bits/stdc++.h>
using namespace std;
int n; string s;
string ans;
bool solve(int l, int r){
if(l == r+1) return 1;
if(s[l] == s[r]){
ans[l] = '('; ans[r] = ')';
return solve(l+1, r-1);
}
stack<int> st; int good = -1;
for(int i = l; i<=r; i++){
if(!st.empty() && st.top() == s[i]-'a') st.pop();
else st.push(s[i]-'a');
if(st.empty() && s[i] == s[l]) good = i;
}
if(good == -1) return false;
ans[l] = '('; ans[good] = ')';
return solve(l+1, good-1)&&solve(good+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);
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 |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 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 |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
3 ms |
492 KB |
Output is correct |
9 |
Correct |
15 ms |
1388 KB |
Output is correct |
10 |
Correct |
7 ms |
1004 KB |
Output is correct |
11 |
Correct |
4 ms |
1132 KB |
Output is correct |
12 |
Correct |
138 ms |
6120 KB |
Output is correct |
13 |
Correct |
64 ms |
5452 KB |
Output is correct |
14 |
Correct |
948 ms |
19180 KB |
Output is correct |
15 |
Correct |
13 ms |
7276 KB |
Output is correct |
16 |
Correct |
13 ms |
7276 KB |
Output is correct |
17 |
Correct |
126 ms |
8044 KB |
Output is correct |
18 |
Correct |
72 ms |
3564 KB |
Output is correct |
19 |
Correct |
1562 ms |
18412 KB |
Output is correct |
20 |
Correct |
628 ms |
15072 KB |
Output is correct |
21 |
Execution timed out |
2084 ms |
16392 KB |
Time limit exceeded |