POJ 2955:brackets interval DP basic problem

Source: Internet
Author: User

Brackets Topic Links:

http://poj.org/problem?id=2955

Test instructions

Give a string consisting only of ' (', ') ', ' [', '] ', the characters can match, the left ' (' can match to the right ') ', the left ' [' can match to the right '] '

The two matches cannot be crossed and can contain, for example, [(]) only 2 matches and [()] count four matches to find the maximum number of matches.

Exercises

Set DP[I][J] for the interval I to J (set Len to interval length, J=i+len) can be obtained in the maximum number of matches, there are two cases:

①j does not match any one of the characters in the interval [i,j] dp[i][j]=dp[i][j-1]

② can find a point in the interval [i,j] k so that the characters on K can match the characters on J, at this time Dp[i][j]=max (dp[i][j],dp[i][k-1]+dp[k+1][j-1]+2)

Code
#include<stdio.h>#include<string.h>Constint N=110;int DP[N][n];Char s[N];IntMmax(int x,int y){return x>y? x: Y;}CharGet_c(Char C){If(c==‘)‘)Return‘(‘;If(c==‘]‘)Return‘[‘;Return‘!‘;}voidSolve(){Memset(DP,0,sizeof(DP));While(~scanf('%s ', S+1) &&s[1]!=E){s[0]=‘?‘;int T=Strlen(s);For(int Len=1; Len<t; ++len)For(int I=1; I+len<=t; ++i){Int J=i+len;Char C=Get_c(s[j]);DP[I][j]=dp[I][j-1];For(int k=i; k<j; ++k)If(s[k]==c){DP[I][j]=Mmax(DP[I][j],dp[I][k-1]+dp[k+1][j-1]+2}}printf (  "%d\n" ,DP [1][t); }}int main () {solve (); return 0;}    

POJ 2955:brackets interval DP basic problem

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.