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