#include<iostream>
#include<vector>
#include<string>
using namespace std;
int n;
string s, ans;
const int N = 2022;
int dp[N][N];
int us[N][N];
int solve(int l, int r) {
if (l > r) return 1;
if (us[l][r]) return dp[l][r];
us[l][r] = 1;
for (int i = r ; i > l ; i -= 2) {
if (s[i] == s[l] && solve(l+1, i-1) && solve(i + 1, r)) {
return dp[l][r] = 1;
}
}
return dp[l][r];
}
void get(int l, int r) {
if (l > r) return;
for (int i = r ; i > l ; i -= 2) {
if (s[i] == s[l] && solve(l+1, i-1) && solve(i + 1, r)) {
ans[l] = '(';
ans[i] = ')';
get(l + 1, i - 1);
get(i + 1, r);
return;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> s;
n = s.size();
if (!solve(0, n-1)) {
cout << -1;
return 0;
}
ans = string(n, '-');
get(0, n - 1);
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
31 ms |
6680 KB |
Output is correct |
5 |
Correct |
5 ms |
5588 KB |
Output is correct |
6 |
Correct |
4 ms |
6416 KB |
Output is correct |
7 |
Correct |
425 ms |
19408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
31 ms |
6680 KB |
Output is correct |
5 |
Correct |
5 ms |
5588 KB |
Output is correct |
6 |
Correct |
4 ms |
6416 KB |
Output is correct |
7 |
Correct |
425 ms |
19408 KB |
Output is correct |
8 |
Runtime error |
230 ms |
34312 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |