Question:
The two matching brackets are used to find the longest matching length.
Train of Thought Analysis:
State equation: DP [I] [J] indicates the interval I ~ The longest matching length between J.
Transition equation: DP [I] [J] = max (DP [I + 1] [J], DP [I] [J-1], DP [I + 1] [k-1] + dp [k + 1] [J] + 2 (condition is I, K position match ))
# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # define maxn 105 using namespace STD; char STR [maxn]; int DP [maxn] [maxn]; int main () {While (scanf ("% s", STR + 1 )! = EOF) {If (strcmp (STR + 1, "end") = 0) break; int Len = strlen (STR + 1); memset (DP, 0, sizeof DP); For (INT I = len-1; I> = 1; I --) {for (Int J = I + 1; j <= Len; j ++) {DP [I] [J] = max (DP [I + 1] [J], DP [I] [J-1]); For (int K = I + 1; k <= J; k ++) {If (STR [I] = '(' & STR [k] = ')') | (STR [I] = '[' & STR [k] = ']') DP [I] [J] = max (DP [I] [J], DP [I + 1] [k-1] + 2 + dp [k + 1] [J]); }}printf ("% d \ n ", DP [1] [Len]);} return 0 ;}