http://poj.org/problem?id=2955
The main idea is to give you a string, which consists of brackets and parentheses, and asks how long the longest one in the string matches the mathematical brace matching specification.
At first, I intend to use the legend of the left closed to open the interval to write, and later found that it is not suitable for me, or to return to the left closed right closed interval write.
DP's idea is simple, dp[i][j] represents the oldest sequence of strings that match parentheses from I to J. For any interval there can be a point K (i <= K < j) dividing the interval into [I, K] and [K+1, J] two parts.
So the transfer equation: dp[i][j] = max (Dp[i][k] + dp[k+1][j])
Also, if the parentheses of I and J match, there is one more case, dp[i][j] = max (dp[i-1][j+1] + 2)
And then the memory is on the line. (It feels like the problem is still more memorable.)
#include <cstring>#include<iostream>#include<algorithm>using namespacestd;stringA;intdp[101][101];BOOLJudgeCharACharb) {if(A = ='('&& b = =')')return true; if(A = ='['&& b = =']')return true; return false;}intCpointLintr) {if(Dp[l][r]! =-1) { returnDp[l][r]; } if(L = =r) {returnDP[L][R] =0; } intTMP =0; for(intK=l; k<r; k++) {tmp= MAX (TMP, DP (l, K) + DP (k +1, R)); } if(Judge (A[l], a[r]) TMP = MAX (tmp, DP (l +1, R-1) +2); returnDP[L][R] =tmp;}intMain () { while(Cin >>a) {if(A = ="End") Break; Memset (DP,-1,sizeof(DP)); intAns = DP (0, A.size ()-1); cout<< ans <<Endl; } return 0;}
View Code
POJ 2955 Brackets interval dp