Codeforces 629C famil Door and Brackets (DP + enumeration)

Source: Internet
Author: User

Topic Links:

Codeforces 629C famil Door and Brackets

Title Description:

Given the full parenthesis sequence length n, now give a sequence s length of M. Enumeration string p,q, so that p+s+q is a valid bracket string, the length is N, ask p,q the number of values.

Problem Solving Ideas:

The length of the enumeration p can be directly obtained by the length of Q. Because you want to satisfy the number of ' (' number greater than ') in any position, count the minimum number of ' ('-') ' in S-string minx. DP[I][J] Represents the number of "('-'-') ' = = j" When the position I is met. The J (i >-minx) of P is then enumerated, and then the Q string is introduced according to the P string to satisfy the state of the legal string, multiplying the two.

1#include <bits/stdc++.h>2 using namespacestd;3 4 typedef __int64 LL;5 Const intINF = 1e9 +7;6 Const intMAXN =100010;7 Const intN =2100;8 Const intMoD = 1e9+7;9 Ten  One ///Dp[i][j] to the position I, there is a J unbalance (brackets A LL Dp[n][n], N, m; - CharSTR[MAXN]; -  the voidInit () - { -     intnum = .; -Memset (DP,0,sizeof(DP)); +dp[0][0] =1; -  +      for(intI=1; i<=num; i++) A          for(intj=0; j<=i; J + +) at         { -             if(J >0) -DP[I][J] = (Dp[i][j] + dp[i-1][j-1]) %MoD; -DP[I][J] = (Dp[i][j] + dp[i-1][j+1]) %MoD; -         } - } in  - LL Solve () to { +     intMinx = INF, TMP =0, num = n-m; -  the      for(intI=0; Str[i]; i++) *     { $         if(Str[i] = ='(')Panax NotoginsengTMP + +; -         Else theTMP--; +Minx =min (Minx, TMP); A     } the  +LL ans =0; -      for(intI=0; i<=num; i++) $          for(intj=0; j<=i; J + +) $             if(J >=-minx && num-i >= j +tmp) -Ans = (ans + dp[i][j]*dp[num-i][j+tmp])%MoD; -  the     returnans; - }Wuyi  the intMain () - { Wu init (); -      while(SCANF ("%i64d%i64d", &n, &m)! =EOF) About     { $scanf ("%s", str); -printf ("%i64d\n", Solve ()); -     } -     return 0; A}

Codeforces 629C famil Door and Brackets (DP + enumeration)

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.