#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 105;
const int INF = 0x3fffffff;
char str[N];
int dp[N][N];
int path[N][N];
//Accepted 240K 16MS C++ 1674B 2014-04-13 15:01:32
void print(int i, int j) {
if(i>j) return;
if(i==j) {
if(str[i]=='['||str[i]==']') printf("[]");
else printf("()");
}
else if(path[i][j]==-1) {
printf("%c", str[i]);
print(i+1, j-1);
printf("%c", str[j]);
}
else {
int k = path[i][j];
print(i, k);
print(k+1, j);
}
}
int main()
{
while(gets(str))
{
int n = strlen(str);
if(n==0) {
printf("\n");
continue;
}
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i++) dp[i][i] = 1;
for(int r=1; r<n; r++) {
for(int i = 0; i < n-r; i++) {
int j = i+r;
dp[i][j] = INF;
if((str[i]=='['&&str[j]==']')||(str[i]=='('&&str[j]==')')) {
dp[i][j] = dp[i+1][j-1];
path[i][j] = -1;
}
for(int k = i; k < j; k++) {
if(dp[i][j]>dp[i][k]+dp[k+1][j]) {
dp[i][j] = dp[i][k]+dp[k+1][j];
path[i][j] = k;
}
}
}
}
#ifdef Debug
for(int i = 1; i < n; i++) {
for(int j=0; j < n-i; j++) {
int s = j, e = j+i;
printf("dp[%d][%d] = %d\n", s, e, dp[s][e]);
}
getchar();
}
#endif
print(0, n-1);
printf("\n");
}
return 0;
}
/***
()()
(]((]][]
([(]
((([])
***/
PS:经典的区间DP,同类有男人八题中的石子合并问题,矩阵相乘问题。