Codeferces 149D Coloring Brackets Interval DP

Source: Internet
Author: User

Http://codeforces.com/problemset/problem/149/D

The topic is basically to give you a string of strings, only parentheses, and already conform to the parentheses matching rules, now you want to paint these brackets, give some coloring rules, to find the number of coloring scheme.

1: The brackets are either not painted or painted blue or painted red.

2: Two matching parentheses are available and only one is painted.

3: Adjacent two brackets may not have the same color.

Here of course also think of the area [L, R] DP, but there is a dependency on the color, so also need to remember the state of the L and R color, initially intended to use only one-dimensional record two points of color, and later found that I was still too young

Thus Dp[l][r][a][b] indicates that the color scheme on the interval [L, R] is a and B (a, B value 0~2, respectively, that is not painted, painted blue, painted red).

Now, in the context of discussion,

For the interval [L, R] if L R matches each other (this is a match in the whole string), the sub-problem is the interval [l+1, r-1] in the coloring of the I, J-state of the and. i.e. Dp[i][j][a][b] = SUM (dp[l+1][r-1][i][j]); Among them, the coloring scheme [I, j] is legal with respect to the scheme [a, b].

If l R does not match each other, then K is the matching bracket of the L bracket, then the interval is split into [L, K] and [K+1, R], so we enumerate K and k+1 coloring states are I, J. The answer is for the scheme [a, I] is valid for the scheme [J, b]. i.e. Dp[l][r][a][b] = SUM (Dp[l][k][a][i] *dp[k+1][r][j][b]);

The corresponding matching brackets for each position can be simulated with a stack. Then a memory search is performed on the interval. For L and R matching intervals, it is also necessary to determine whether their coloring scheme is legal (because the mismatched interval is not good to judge, and eventually divided into several matching intervals to judge, so it is not judged). Also, the topic requires the 10^9 + 7 modulo.

#include <stack> #include <cstring> #include <iostream> #include <algorithm>using namespace std ; const long Long MOD = 1000000007ll;stack<int > S;        int v[707];void doit (string a) {for (int i=0; i<a.size (); i++) {if (a[i] = = ' (') {s.push (i);            } else {int tmp = S.top ();            S.pop ();            V[TMP] = i;        V[i] = tmp; }}}long long Dp[707][707][3][3];long long dp (int l, int r, int a, int b) {if (v[l] = = R && ((a = = b) | | (A! = 0 && B! = 0)))    {return dp[l][r][a][b] = 0;    } if (Dp[l][r][a][b]! =-1) {return dp[l][r][a][b]% MOD;    } if (r-l = = 1) {return dp[l][r][a][b] = 1;        A long long tmp = 0;        if (v[l]! = r) {int k = v[l];  for (int i=0, i<3; i++) {for (int j=0; j<3; J + +) {if ((i = = 0 && J = = 0) | | I! = j) {TMP = (tmp + DP (l, K, A, i) * DP (K+1, R, J,b))% MOD;                }}}} else {for (Int. i=0; i<3; i++) {for (int j=0; j<3; J + +) { if ((a = = 0 && J! = b) | | (I! = a && b = = 0))                {TMP = (tmp + DP (l+1, R-1, I, j))% MOD; }}}} (return dp[l][r][a][b] = tmp% MOD;}    int main () {string A;    Cin >> A;    Doit (a);    Memset (DP,-1, sizeof (DP));    A long long ans = 0;        for (int i=0; i<3; i++) {for (int j=0; j<3; J + +) {ans = (ans + DP (0, A.size ()-1, I, j))% MOD;    }} cout << ans% MOD << Endl; return 0;}


Codeferces 149D Coloring Brackets Interval 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.