#include <bits/stdc++.h>
using namespace std;
// subtask 2
int n, cnt[2005];
char res[2005];
string s;
void solve(int l, int r) {
if (l > r) return;
stack<char> st;
for (int i = l + 1; i <= r; ++i) {
cnt[i] = 0;
if (!st.size()) {
st.push(s[i]), cnt[i] += s[i] == s[l];
}
else {
if (st.top() == s[i]) st.pop();
else st.push(s[i]);
}
}
while (st.size()) st.pop();
for (int i = r; i >= l + 1; --i) {
if (!st.size()) {
st.push(s[i]), cnt[i] += s[i] == s[l];
}
else {
if (st.top() == s[i]) st.pop();
else st.push(s[i]);
}
}
int pos = 0;
for (int i = l + 1; i <= r; ++i) {
if (cnt[i] == 2) pos = i;
}
if (!pos) {
cout << -1; exit(0);
}
res[l] = '(', res[pos] = ')';
solve(l + 1, pos - 1), solve(pos + 1, r);
}
int main() {
ios::sync_with_stdio(false);
cin >> s, n = s.size();
if (n > 2000) return 0;
solve(0, n - 1);
for (int i = 0; i < n; ++i) cout << res[i];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2184 KB |
Output is correct |
2 |
Correct |
0 ms |
2184 KB |
Output is correct |
3 |
Correct |
0 ms |
2184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2184 KB |
Output is correct |
2 |
Correct |
0 ms |
2184 KB |
Output is correct |
3 |
Correct |
0 ms |
2184 KB |
Output is correct |
4 |
Correct |
0 ms |
2316 KB |
Output is correct |
5 |
Correct |
0 ms |
2184 KB |
Output is correct |
6 |
Correct |
6 ms |
2632 KB |
Output is correct |
7 |
Correct |
3 ms |
2316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2184 KB |
Output is correct |
2 |
Correct |
0 ms |
2184 KB |
Output is correct |
3 |
Correct |
0 ms |
2184 KB |
Output is correct |
4 |
Correct |
0 ms |
2316 KB |
Output is correct |
5 |
Correct |
0 ms |
2184 KB |
Output is correct |
6 |
Correct |
6 ms |
2632 KB |
Output is correct |
7 |
Correct |
3 ms |
2316 KB |
Output is correct |
8 |
Incorrect |
0 ms |
2184 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |