이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 20;
char s[MAXN];
bool dp[MAXN][MAXN];
char ans[MAXN];
void solve(int l, int r) {
if(l > r) return;
for(int i = r; ; i--) if(s[l] == s[i] && dp[l + 1][i - 1] && dp[i + 1][r]) {
ans[l] = '(';
ans[i] = ')';
solve(l + 1, i - 1);
solve(i + 1, r);
return;
}
}
int main() {
int N;
cin >> s;
for(N = 0; s[N]; N++);
for(int i = 0; i < N; i++) dp[i + 1][i] = true;
for(int i = 2; i <= N; i++) for(int j = 0; i + j - 1 < N; j++) {
for(int k = j + 1; k <= i + j - 1; k++) if(s[j] == s[k] && dp[j + 1][k - 1] && dp[k + 1][i + j - 1])
dp[j][i + j - 1] = true;
}
if(!dp[0][N - 1]) cout << -1;
else {
solve(0, N - 1);
for(int i = 0; i < N; i++) cout << ans[i];
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |