Topic Link: Portal
Topic: Chinese problem, slightly
Topic Idea: Interval DP
The question is how many parentheses are needed to make it a valid parenthesis sequence, so we can ask how many valid parentheses to match, and then subtract the number of matching parentheses by the length of the string.
The state transition equation is mainly for the interval dp[i][j of our enumeration], if the brackets at the I and J can match, then dp[i][j]=dp[i+1][j-1]+1;
Because we are from small to large enumeration length, so the interval of small length must be optimal, so when I and J match, it is the optimal value of sub-interval +1
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<cstring>#include<stack>#include<cctype>#include<queue>#include<string>#include<vector>#include<Set>#include<map>#include<climits>#defineLson Root<<1,l,mid#defineRson Root<<1|1,mid+1,r#defineFi first#defineSe Second#definePing (x, y) ((x-y) * (x-y))#defineMST (x, y) memset (x,y,sizeof (x))#defineMCP (x, y) memcpy (x,y,sizeof (y))using namespacestd;#defineGamma 0.5772156649015328606065120#defineMOD 1000000007#defineINF 0x3f3f3f3f#defineN 100005#defineMAXN 1050typedef pair<int,int>Pii;typedefLong LongLL;intN,m,cnt,temp,ans;Charstr[ +];intdp[ the][ the];intMain () {intI,j,group,case=0; while(SCANF ("%s", str+1)!=EOF) {MST (DP,0); intLen=strlen (str+1); for(intp=2;p <=len;++p) for(i=1; i<=len;++i) {J=i+p-1;if(J>len) Break; if((str[i]=='('&&str[j]==')')|| (str[i]=='['&&str[j]==']')) dp[i][j]=dp[i+1][j-1]+1; for(intk=i;k<j;++k) {Dp[i][j]=max (dp[i][j],dp[i][k]+dp[k+1][j]); }} printf ("%d\n", len-dp[1][len]*2); } return 0;}
Codevs (3657-bracket sequence)