Description
Let us define a regular brackets sequence in the following way:
1. Empty sequence is a regular sequence.
2. If S is a regular sequence, then (S) and [S] are both regular sequences.
3. If A and B are regular sequences, then AB is a regular sequence.
For example, all of the following sequences of characters are regular brackets sequences:
(), [], (()), ([]), ()[], ()[()]
And all of the following character sequences are not:
(, [, ), )(, ([)], ([(]
Some sequence of characters '(', ')', '[', and ']' is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m, that aj = bij for all 1 = j = n.
1. Empty sequence is a regular sequence.
2. If S is a regular sequence, then (S) and [S] are both regular sequences.
3. If A and B are regular sequences, then AB is a regular sequence.
For example, all of the following sequences of characters are regular brackets sequences:
(), [], (()), ([]), ()[], ()[()]
And all of the following character sequences are not:
(, [, ), )(, ([)], ([(]
Some sequence of characters '(', ')', '[', and ']' is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m, that aj = bij for all 1 = j = n.
Input
The input file contains at most 100 brackets (characters '(', ')', '[' and ']') that are situated on a single line without any other characters among them.
Output
Write to the output file a single line that contains some regular brackets sequence that has the minimal possible length and contains the given sequence as a subsequence.
Sample Input
([(]
Sample Output
()[()]
/*
初看想用栈。发现实现不了。。看题解
d[i][j]表示子串i到j最少需要添加的括号数
形如(S)或者[S]:dp[i][j]=dp[i+1][j-1];
形如(S 或者 [S:dp[i][j]=dp[i+1][j]+1;
形如 S) 或者 S]:dp[i][j]=dp[i][j-1]+1;
如果长度大于1:dp[i][j]=dp[i][k]+dp[k+1][j];(k>=i&&k<=j)
初始化dp[i][i]=1;显然第二第三步已经被第四步涵盖。所以可以合并
这题要注意的是dp[i][j]要可能用到dp[i+1][j-1],dp[i+1][j],dp[i][j-1]这3个。这要求我们在求
得dp[i][j]之前必须知道这三个东西。我的处理方法是i循环从大到小。
j循环从小到大。。。
输出整串。看题解的。这递归用得好巧。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 108
#define inf 0x3f3f3f3f
char A[maxn];
int dp[maxn][maxn];
int path[maxn][maxn];
void print(int a,int b)
{
if(a>b)return;
else if(a==b)
{
if(A[a]=='('||A[a]==')')printf("()");
else printf("[]");
return;
}
else
{
if(path[a][b]==-1)
{
printf("%c",A[a]);
print(a+1,b-1);
printf("%c",A[b]);
}
else
{
print(a,path[a][b]);
print(path[a][b]+1,b);
}
}
}
int main()
{
while(gets(A))
{
int len=strlen(A);
memset(dp,0,sizeof(dp));
memset(path,0,sizeof(path));
for(int i=0;i<len;i++)
{
dp[i][i]=1;
}
for(int i=len-1;i>=0;i--)
{
for(int j=1;j<=len-i-1;j++)
{
int z=i+j;
if(z==i)continue;
dp[i][z]=inf;
if((A[i]=='('&&A[z]==')')||(A[i]=='['&&A[z]==']'))
{
if(dp[i][z]>dp[i+1][z-1])
{
dp[i][z]=dp[i+1][z-1];
path[i][z]=-1;
}
}
for(int k=i;k<z;k++)
{
if(dp[i][z]>dp[i][k]+dp[k+1][z])
{
dp[i][z]=dp[i][k]+dp[k+1][z];
path[i][z]=k;
}
}
}
}
print(0,len-1);
printf("\n");
}
return 0;
}