#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;
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 |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
904 KB |
Output is correct |
7 |
Correct |
2 ms |
620 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 |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
904 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
4 ms |
492 KB |
Output is correct |
9 |
Correct |
33 ms |
2060 KB |
Output is correct |
10 |
Correct |
24 ms |
1536 KB |
Output is correct |
11 |
Correct |
35 ms |
3180 KB |
Output is correct |
12 |
Correct |
1461 ms |
27484 KB |
Output is correct |
13 |
Correct |
1596 ms |
33704 KB |
Output is correct |
14 |
Execution timed out |
2082 ms |
29784 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |