Codevs (3657-bracket sequence)

Source: Internet
Author: User

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)

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.