To find the maximum number of matches for a brace, this question can be compared to UVA 1626.
Pay attention to the same background, but do not ask for the same.
Back to this question, set D (i, j) to denote the maximum number of matches in a substring of SI ~ sj, if Si and SJ can pair, D (i, j) = d (i+1, j-1)
Then to enumerate the intermediate points k,d (i, j) = max{D (i, K) + D (k+1, J)}
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 7 Const intMAXN = -+Ten;8 9 intN;Ten CharS[MAXN]; One intD[MAXN][MAXN]; A - BOOLInline Match (CharC1,CharC2) - { the if(C1 = ='['&& C2 = =']')return true; - if(C1 = ='('&& C2 = =')')return true; - return false; - } + - intMain () + { A while(SCANF ("%s", s) = =1&& s[0] !='e') at { -n =strlen (s); - for(inti =0; i +1< n; i++) - { - if(Match (S[i], s[i+1])) d[i][i+1] =2; - Elsed[i][i+1] =0; in } - to for(intL =3; L <= N; l++) + { - for(inti =0; i + L-1< n; i++) the { * intj = i + L-1; $D[I][J] =0;Panax Notoginseng if(Match (S[i], s[j])) d[i][j] = d[i+1][j-1] +2; - for(intK = i; K < J; k++) theD[I][J] = max (D[i][j], d[i][k] + d[k+1][j]); + } A } the +printf"%d\n", d[0][n-1]); - } $ $ return 0; -}
code June
POJ 2955 interval DP Brackets