Topic Link: Click to open the link
Test instructions: give you a length of M only contains () of the bracket string s, requires at the end of the s on the addition of two strings p and Q, so that the total length of n, and balance, the balance refers to any prefix string (not less than), and the whole string of (and) as many.
Idea: It is not difficult for us to think of such a DP, D[i][j] represents a string of length I, (than) more than J (or) more than (more than J, is equivalent) of the scheme number. So the transfer is simple:
if (J > 0) d[i][j] + = d[i-1][j-1]; D[I][J] + = d[i-1][j+1].
Then in order to satisfy those two conditions, we calculate the minimum balance of S string minx, and the balance of the whole string s cur, enumerate the length and balance of the first string, then we can calculate the length and balance of the second string, so we can accumulate the answer.
See the code for details:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue># Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;typedef long long ll;typedef long double ld;const ld EPS = 1e-9, PI = 3.14159265358979323846264 33832795;const int mod = 1000000000 + 7;const int INF = int (1e9), const LL INF64 = LL (1e18); const int MAXN = + + 10;int T,n,m;ll d[maxn][maxn];void Add (ll &a, ll b) {A + = B; if (a >= mod) A-= mod;} char s[(int) (1E5 +)];int main () {scanf ("%d%d", &n,&m); scanf ("%s", s+1); memset (d, 0, sizeof (d)); D[0][0] = 1; for (int i=1;i<=n-m;i++) {for (int j=0;j<=i;j++) {if (J > 0) {Add (D[i][j], d[i-1 ][j-1]); } Add (D[i][j], d[i-1][j+1]); }} int minx = INF; int cur = 0; for (int i=1;i<=m;i++) {if (s[i] = = ' (') ++cur; else--cur; Minx = min (minx, cur); } ll ans = 0; for (int i=0;i<=n-m;i++) {for (int j=0;j<=i;j++) {if (j + Minx >= 0 && j + cur <= n-m -i) {Add (ans, d[i][j] * d[n-m-i][j+cur]% mod); }}} printf ("%i64d\n", ans); return 0;}
Codeforces Round #343 (Div. 2) (C. Famil Door and Brackets (DP))