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)