char str[300]; int dp[300][300]; int path[300][300];
voidoprint(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]); oprint(i + 1,j - 1); printf("%c", str[j]); }else { oprint(i, path[i][j]); oprint(path[i][j] + 1, j); } } int main(){ while (gets(str)){ int n = strlen(str); if (n == 0) {printf("n");continue;} memset(dp,0,sizeof(dp)); memset(path,-1,sizeof(path)); for (int i = 0; i < n; i ++) dp[i][i] = 1; for (int len = 1; len < n; len ++){ for (int i = 0; i < n - len; i ++){ int j = i + len;//末尾 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]){ path[i][j] = k; dp[i][j] = dp[i][k] + dp[k + 1][j]; } }