Codeforces Round #343 (Div. 2) (C. Famil Door and Brackets (DP))

Source: Internet
Author: User

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))

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.