#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int MAXN = 100 + 10;
int dp[MAXN][MAXN];
char s[MAXN];
int path[MAXN][MAXN];
void output(int l, int r)
{
if(l > r) return ;
else if(l == r)
{
if(s[l] == '(' || s[l] == ')')
printf("()");
else
printf("[]");
}
else if(path[l][r] == -1)
{
printf("%c", s[l]);
output(l+1,r-1);
printf("%c", s[r]);
}
else
{
output(l, path[l][r]);
output(path[l][r] + 1, r);
}
}
int main()
{
scanf("%s", s+1);
memset(dp,0,sizeof(dp));
int n = strlen(s + 1);
for(int i=1;i<=n;i++) dp[i][i] = 1;
for(int l=2;l<=n;l++)
{
for(int i=1;i<=n-l+1;i++)
{
int j = i + l - 1;
if((s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']'))
{
dp[i][j] = dp[i+1][j-1];
path[i][j] = -1;
}
else
dp[i][j] = 0x3f3f3f3f3f;
for(int k=i;k<j;k++)
{
if(dp[i][k] + dp[k+1][j] < dp[i][j])
{
dp[i][j] = dp[i][k] + dp[k+1][j];
path[i][j] = k;
}
}
}
}
output(1, n);
printf("\n");
return 0;
}
POJ 1141 括号匹配加强版
最新推荐文章于 2022-08-08 14:25:20 发布