-
Notifications
You must be signed in to change notification settings - Fork 0
/
1463.cpp
88 lines (88 loc) · 1.21 KB
/
1463.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<cstdio>
#include<cstring>
int n;
const int inf=9999999;
char s[110];
int dp[110][110],p[110][110];
void print(int i,int j)
{
if(i>j)
return;
if(i==j)
{
if(s[i]=='('||s[i]==')')
printf("()");
else
printf("[]");
return;
}
else if(p[i][j]==-1)
{
printf("%c",s[i]);
print(i+1,j-1);
printf("%c",s[j]);
return;
}
else
{
int k=p[i][j];
print(i,k);
print(k+1,j);
return;
}
}
int main()
{
scanf("%d",&n);
getchar();
getchar();
while(n--)
{
gets(s);
if(s[0]=='\0')
{
puts("");
if(n)
printf("\n");
continue;
}
int l=strlen(s);
for(int i=0;i<l;i++)
for(int j=0;j<l;j++)
{
if(i>j)
dp[i][j]=0;
else
if(i==j)
dp[i][j]=1;
else
dp[i][j]=inf;
}
for(int len=2;len<=l;len++)
for(int i=0;i+len-1<l;i++)
{
int j=i+len-1;
if((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
{
dp[i][j]=dp[i+1][j-1];
p[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];
p[i][j]=k;
}
}
}
print(0,l-1);
printf("\n");
if(n)
{
printf("\n");
getchar();
}
}
return 0;
}