1 //#include <bits/stdc++.h> 2 #include<iostream> 3 #include<stdio.h> 4 #include<string.h> 5 using namespace std; 6 char s[128]; 7 int dp[128][128]; 8 int n; 9 10 int dfs(int l,int r) 11 { 12 if(dp[l][r]!=-1) 13 return dp[l][r]; 14 if(l==r) 15 return dp[l][r]=0; 16 if(l+1==r){ 17 if(s[l]=='('&&s[r]==')') 18 return dp[l][r]=2; 19 if(s[l]=='['&&s[r]==']') 20 return dp[l][r]=2; 21 } 22 //int maxx=0; 23 if(s[l]=='('&&s[r]==')') 24 //maxx=max(maxx,2+rec(l+1,r-1)); 25 dp[l][r] = max(dp[l][r], dfs(l + 1, r - 1) + 2); 26 if(s[l]=='['&&s[r]==']') 27 // maxx=max(maxx,2+rec(l+1,r-1)); 28 dp[l][r] = max(dp[l][r], dfs(l + 1, r - 1) + 2); 29 for(int k=l;k<=r-1;++k) 30 //maxx=max(maxx,rec(l,k)+rec(k+1,r)); 31 dp[l][r] = max(dp[l][r], dfs(l, k) + dfs(k + 1, r)); 32 return dp[l][r]; 33 } 34 35 int main() 36 { 37 while(scanf("%s",s)==1){ 38 if(s[0]=='e') 39 break; 40 memset(dp,-1,sizeof(dp)); 41 n=strlen(s); 42 printf("%d ",dfs(0,strlen(s)-1)); 43 } 44 return 0; 45 }