Poj 2955 Brackets interval dp

Source: Internet
Author: User

Poj 2955 Brackets interval dp

 

The question is to give you a string consisting of braces and parentheses. Ask how long the longest subsequence in the string conforms to the matching rules of mathematical parentheses.

At the beginning, I plan to use the legendary left-closed and right-open interval to write data. Later I found it was not suitable for me, but I still wrote back the left-closed and right-closed interval.

The idea of dp is relatively simple. dp [I] [j] indicates that the string from I to j is the oldest sequence matching brackets. For any interval, a vertex k (I <= k <j) can divide the interval into two parts: [I, k] and [k + 1, j.

So we get the transfer equation: dp [I] [j] = max (dp [I] [k] + dp [k + 1] [j])

In addition, if the parentheses of I and j match, there is another situation here, that is, dp [I] [j] = max (dp [I-1] [j + 1] + 2)

Then the memory will work. (I feel like this question is more memorable)

 

 

#include 
 
  #include 
  
   #include using namespace std;string a;int dp[101][101];bool judge(char a, char b) {    if (a == '(' && b == ')') return true;    if (a == '[' && b == ']') return true;    return false;}int DP(int l, int r) {    if (dp[l][r] != -1) {        return dp[l][r];    }www.bkjia.com    if (l == r) {        return dp[l][r] = 0;    }    int tmp = 0;    for (int k=l; k
   
    > a) {        if (a == end) break;        memset(dp, -1, sizeof(dp));        int ans = DP(0, a.size()-1);        cout << ans << endl;    }    return 0;}
   
  
 


 

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.