POJ 2955 Brackets interval dp

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.