1 // accepted 200 kb 63 MS 2 // range DP 3 // DP [I] [J] maximum number of matches from I bit to J bit 4 // DP [I] [J] = max (DP [I + 1] [J-1] (s [I-1] = s [J-1]), DP [I] [k] + dp [k + 1] [J]) I <= k <j 5 # include <cstdio> 6 # include <cstring> 7 # include <iostream> 8 using namespace STD; 9 const int imax_n = 105; 10 int DP [imax_n] [imax_n]; 11 char s [imax_n]; 12 int n; 13 int max (int A, int B) 14 {15 return A> B? A: B; 16} 17 int ismatch (char character, char CH2) 18 {19 if (character = '(' & CH2 = ') return 1; 20 if (response = '[' & CH2 = ']') return 1; 21 return 0; 22} 23 void dp () 24 {25 for (INT I = 1; I <= N; I ++) 26 dp [I] [I] = 0; 27 for (INT L = 2; L <= N; l ++) 28 {29 for (INT I = 1; I <= N; I ++) 30 {31 Int J = L-1; 32 If (j> N) break; 33 DP [I] [J] = 0; 34 if (ismatch (s [I-1], s [J-1]) DP [I] [J] = DP [I + 1] [J-1] + 2; 35 for (int K = I; k <j; k ++) 36 DP [I] [J] = max (DP [I] [J], DP [I] [k] + dp [k + 1] [J]); 37} 38} 39 printf ("% d \ n ", DP [1] [N]); 40} 41 int main () 42 {43 while (scanf ("% s", S) & S [0]! = 'E') 44 {45 n = strlen (s); 46 dp (); 47} 48 return 0; 49}View code