1 #include <cstdio>
2 #include <cstring>
3 #define N 105
4
5 int dp[N][N], len, vis[N][N];
6 char s[N];
7
8 int max2(int x, int y)
9 {
10 return x > y ? x : y;
11 }
12
13 int dfs(int i, int j)
14 {
15 if(i>=j || i<1 || j>len) return 0;
16 if(vis[i][j]==1) return dp[i][j];
17 dp[i][j] = dfs(i+1, j);
18 if(s[i]==‘(‘)
19 {
20 for(int x=i+1; x<=j; x++) //x++写成j++
21 {
22 if(s[x]==‘)‘)
23 {
24 dp[i][j] = max2(dp[i][j], dfs(i+1,x-1) + dfs(x+1, j) + 2);
25 }
26 }
27 }
28 else if(s[i]==‘[‘)
29 {
30 for(int x=i+1; x<=j; x++)
31 {
32 if(s[x]==‘]‘)
33 {
34 dp[i][j] = max2(dp[i][j], dfs(i+1,x-1) + dfs(x+1, j) + 2);
35 }
36 }
37 }
38
39 vis[i][j] = 1;
40 return dp[i][j];
41 }
42
43 int main()
44 {
45 while(scanf("%s",s+1)!=EOF)
46 {
47 if(strcmp(s+1,"end")==0)
48 break;
49 len = strlen(s+1);
50 memset(vis, 0, sizeof(vis));
51 memset(dp, 0, sizeof(dp));
52
53 dfs(1,len);
54 printf("%d\n",dp[1][len]);
55 }
56 return 0;
57 }