#include "bits/stdc++.h"
using namespace std;
string s;
vector<int> ans;
inline bool rec (int l, int r) {
if (l > r) return true;
if (l == r) return false;
if (s[l] == s[r]) return ans[l] = '(', ans[r] = ')', rec(l + 1, r - 1);
stack<char> stk;
stk.push(s[r]);
for (int i = r - 1; i > l; --i) {
if (stk.empty() || stk.top() != s[i]) stk.push(s[i]);
else stk.pop();
if (s[i - 1] == s[l] && stk.empty()) return ans[l] = '(', ans[i - 1] = ')', rec(l + 1, i - 2) & rec(i, r);
}
return false;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> s;
ans.resize(s.size());
bool ok = rec(0, (int) s.size() - 1);
if (ok) for (char c : ans) cout << c;
else cout << -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
9 ms |
980 KB |
Output is correct |
10 |
Correct |
4 ms |
584 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
72 ms |
2020 KB |
Output is correct |
13 |
Correct |
33 ms |
1328 KB |
Output is correct |
14 |
Correct |
476 ms |
7116 KB |
Output is correct |
15 |
Correct |
3 ms |
980 KB |
Output is correct |
16 |
Correct |
4 ms |
996 KB |
Output is correct |
17 |
Correct |
71 ms |
1344 KB |
Output is correct |
18 |
Correct |
7 ms |
1104 KB |
Output is correct |
19 |
Correct |
810 ms |
7756 KB |
Output is correct |
20 |
Correct |
311 ms |
5700 KB |
Output is correct |
21 |
Correct |
1702 ms |
12572 KB |
Output is correct |