Angry take a blood, first blood, the first interval DP, the first time it was like this, it was inexplicably lost ~~~
Although the question is a bird's language, it is still very naked to tell us that the maximum number of matching brackets is required. DP starts from the beginning ~
DP [I] [J] indicates the maximum number of matches in the interval [I, j ].State transition equationThat is:
DP [I] [J] = max (DP [I] [k] + dp [k + 1] [J])
Yes, firstInitialization BoundaryAh, two steps ~ :
Memset (DP, 0, sizeof DP );
If STR [I] = STR [I + 1]: DP [I] [I + 1] = 2 please refer to ----> This string ([] [] [) it's easy to understand.
Malicious Code:
# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> using namespace STD; int DP [110] [110]; char s [110]; bool check (int I, Int J) // judge whether it matches {If (s [I] = '[' & S [J] = ']') return true; If (s [I] = '(' & S [J] = ') return true; return false;} int main () {While (scanf ("% s", S )! = EOF) {If (strcmp (S, "end") = 0) break; int L = strlen (s); memset (DP, 0, sizeof DP ); for (INT I = 0; I <L; I ++) {// initialize if (check (I, I + 1 )) {DP [I] [I + 1] = 2 ;}} for (INT P = 3; P <= L; P ++) {// enumeration Interval Length for (INT I = 0; I <= L-P; I ++) {// enumeration interval start point Int J = I + p-1; if (check (I, j) {DP [I] [J] = DP [I + 1] [J-1] + 2;} For (int K = I; k <j; k ++) {// divide the interval into two segments DP [I] [J] = max (DP [I] [J], DP [I] [k] + dp [k] [J]) ;}} cout <DP [0] L-1] <Endl ;}return 0 ;}