#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#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;
}
Compilation message
match.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
2 | #pragma GCC optimization ("O3")
|
match.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
# |
결과 |
실행 시간 |
메모리 |
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 |
364 KB |
Output is correct |
6 |
Correct |
3 ms |
876 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 |
364 KB |
Output is correct |
6 |
Correct |
3 ms |
876 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 |
2028 KB |
Output is correct |
10 |
Correct |
21 ms |
1644 KB |
Output is correct |
11 |
Correct |
34 ms |
3180 KB |
Output is correct |
12 |
Correct |
1366 ms |
27324 KB |
Output is correct |
13 |
Correct |
1509 ms |
33864 KB |
Output is correct |
14 |
Execution timed out |
2076 ms |
30132 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |