From left to right, If you first encounter '(and then') 'or first encounter' ['and then'] ', the matching length is 2.
Assume that the length of a string is Len.
0 ...... (len-1)
Evaluate the maximum length of matching parentheses for any substring from I to J subscript.
Set to f (I, j)
Then f (I, j) = max (F [I, K] + F [k + 1] [J] (enumeration ), f [I + 1] [J-1] + (A [I], a [J] matching? 2: 0 ));
OK, dynamic grouping, starting from the substate
When the boundary is Len = 1 and Len = 2, the DP values are 0, 2, and 0, respectively.
# Include <iostream> # include <string. h> # include <cstring> # define max (a, B) (a)> (B )? (A) :( B) using namespace STD; const int maxn = 110; char STR [maxn]; int DP [maxn] [maxn]; bool is_match (const char &, const char & B) {if (a = '(' & B = ') return true; if (A = '[' & B = ']') return true; return false;} int main (void) {While (CIN> Str) {If (STR [0] = 'E') break; int I, j, k; int Len = strlen (STR); memset (DP, 0, sizeof (DP); for (I = 0; I <Len; I ++) {DP [I] [I] = 0; If (is_match (STR [I], STR [I + 1]) DP [I] [I + 1] = 2; // when Len = 1 and Len = 2, their DP values are 0 and 2, respectively, or 0 else DP [I] [I + 1] = 0 ;} for (k = 3; k <= Len; k ++) {for (I = 0; I + k-1 <Len; I ++) {If (is_match (STR [I], STR [I + k-1]) DP [I] [I + k-1] = DP [I + 1] [I + K-2] + 2; for (j = I; j <I + k-1; j ++) {DP [I] [I + k-1] = max (DP [I] [I + k-1], DP [I] [J] + dp [J + 1] [I + k-1]); }}cout <DP [0] [len-1] <Endl ;} return 0 ;}