Poj 2955 (interval DP)

Source: Internet
Author: User

Question Link: Http://poj.org/problem? Id = 2955

Theme: Matching brackets. The number of symmetric parentheses matches + 2. Maximum number of matches.

Solutions:

It looks like a range problem.

DP border: none. When the interval is 0, the default value is memset 0.

For DP [I] [J], if I and j match, it is not difficult to have DP [I] [J] = DP [I + 1] [J-1] + 2.

Then enumeration does not belong to the midpoint at both ends, DP [I] [J] = max (DP [I] [J], DP [I] [k] + dp [k] [J]), and merge the results of the two intervals.

 

#include "cstdio"#include "string"#include "cstring"#include "iostream"using namespace std;bool check(char a,int b){    if(a==‘(‘&&b==‘)‘) return true;    else if(a==‘[‘&&b==‘]‘) return true;    else return false;}int dp[105][105];int main(){    //freopen("in.txt","r",stdin);    string str;    while(cin>>str&&str!="end")    {        int n=str.size();        for(int p=1;p<n;p++)        {            for(int i=0;i<n-p;i++)            {                int j=i+p;                if(check(str[i],str[j])) dp[i][j]=dp[i+1][j-1]+2;                for(int k=i+1;k<j;k++)                    dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]);            }        }        printf("%d\n",dp[0][n-1]);        memset(dp,0,sizeof(dp));    }}

 

Poj 2955 (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.