Outputs the number of parentheses within a string that can be matched
State transition equation:
if (s[i]== ' (' &&s[j]== ') ' | | s[i]== ' [' &&s[j]== '] ')
dp[i][j]=dp[i+1][j-1]+2;
And then merge the intervals.
1 //#pragma COMMENT (linker, "/stack:167772160")//Manual expansion of the ~~~~HDU with C + + intersection2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <iostream>6#include <queue>7#include <stack>8#include <cmath>9#include <Set>Ten#include <algorithm> One#include <vector> A //#include <malloc.h> - using namespacestd; - #defineCLC (A, B) memset (A,b,sizeof (a)) the #defineLL Long Long - Const intINF =0x3f3f3f3f; - Const DoubleEPS = 1e-5; - Const DoublePI = ACOs (-1); + //Inline int R () { - //int X=0,f=1;char Ch=getchar (); + //While (ch> ' 9 ' | | ch< ' 0 ') {if (ch== '-') F=-1;ch=getchar ();} A //while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} at //return x*f; - // } - intdp[ the][ the]; - Chars[ the]; - intMain () { - //freopen ("In.txt", "R", stdin); in while(Gets (s)! =NULL) { - if(s[0]=='e') Break; to //cout<<s<<endl; + intlen=strlen (s); -CLC (DP,0); the for(inti=len-2; i>=0; i--){ * for(intj=i+1; j<len;j++){ $ if(s[i]=='('&&s[j]==')'|| s[i]=='['&&s[j]==']')Panax Notoginsengdp[i][j]=dp[i+1][j-1]+2; - for(intk=i;k<j;k++)//Interval Merging theDp[i][j]=max (dp[i][j],dp[i][k]+dp[k+1][j]); + } A } theprintf"%d\n", dp[0][len-1]); + } - return 0; $}
POJ 2955 Brackets Zone Merge