#include <cstdio>
#include <cstdlib>
#include <iostream>
#define inf 0x3fffffff
using namespace std;
int f[101][101];
int p[101][101];
char s[102];
void dyna(int n) {
int i, j, k, l, t;
for (i = 1; i <= n; i++)
f[i][i] = 1;
for (l = 2; l <= n; l++) {
for (i = 1; i <= n - l + 1; i++) {
j = i + l - 1;
f[i][j] = inf;
p[i][j] = -1;
if (s[i] == '(' && s[j] == ')' || s[i] == '[' && s[j] == ']')
f[i][j] = f[i + 1][j - 1];
for (k = i; k < j; k++) {
t = f[i][k] + f[k + 1][j];
if (t < f[i][j]) {
f[i][j] = t;
p[i][j] = k;
}
}
}
}
}
void recur(int beg, int end) {
if (beg > end)
return;
if (beg == end) {
if (s[beg] == '(' || s[beg] == ')')
printf("()");
else
printf("[]");
} else {
if (p[beg][end] == -1) {
printf("%c", s[beg]);
recur(beg + 1, end - 1);
printf("%c", s[end]);
} else {
recur(beg, p[beg][end]);
recur(p[beg][end] + 1, end);
}
}
}
int main(int argc, char* argv[]) {
int n;
scanf("%s", s + 1);
n = strlen(s + 1);
dyna(n);
recur(1, n);
printf("\n");
return 0;
}
|