f(i,j)=f(i+1,j-1)+2 or max(f(i,k)+f(k+1,j))
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<vector> 5 #include<queue> 6 #include<cmath> 7 #include<cstring> 8 using namespace std; 9 const int maxn=1e6+5; 10 const int INF=1e9+7; 11 char s[105]; 12 int n,dp[105][105]; 13 template <class t>void red(t &x) 14 { 15 x=0; 16 int w=1; 17 char ch=getchar(); 18 while(ch<'0'||ch>'9') 19 { 20 if(ch=='-') 21 w=-1; 22 ch=getchar(); 23 } 24 while(ch>='0'&&ch<='9') 25 { 26 x=(x<<3)+(x<<1)+ch-'0'; 27 ch=getchar(); 28 } 29 x*=w; 30 } 31 void input() 32 { 33 freopen("input.txt","r",stdin); 34 } 35 int main() 36 { 37 input(); 38 while(1) 39 { 40 scanf("%s",s+1); 41 if(s[1]=='e') 42 break; 43 n=strlen(s+1); 44 memset(dp,0,sizeof(dp)); 45 for(int i=2;i<=n;++i) 46 for(int j=1;j+i<=n+1;++j) 47 { 48 int e=i+j-1; 49 if((s[j]=='('&&s[e]==')')||(s[j]=='['&&s[e]==']')) 50 dp[j][e]=dp[j+1][e-1]+2; 51 for(int k=j;k<e;++k) 52 dp[j][e]=max(dp[j][e],dp[j][k]+dp[k+1][e]); 53 } 54 printf("%d ",dp[1][n]); 55 } 56 return 0; 57 }