CodeForces 149D Coloring Brackets, codeforces149d
Coloring Bracketstime limit per test: 2 secondsmemory limit per test: 256 megabytesinput: standard inputoutput: standard output
Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it.
You are given stringS. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening ("(") and closing (")") brackets, such that it is possible to obtain a correct mathematical expression from it, inserting numbers and operators between the brackets. for example, such sequences as "()" and "()" are correct bracket sequences and such sequences as ")" and "(() "are not.
In a correct bracket sequence each bracket corresponds to the matching bracket (an opening bracket corresponds to the matching closing bracket and vice versa ). for example, in a bracket sequence shown of the figure below, the third bracket corresponds to the matching sixth one and the th bracket corresponds to the fourth one.
1000000007 (10
9 rows + rows 7 ).InputThe first line contains the single stringS(2 bytes ≤ bytes |S| Limit ≤ limit 700) which represents a correct bracket sequence.
OutputPrint the only number-the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007 (109 rows + rows 7 ).
ExamplesInput(())
Output12
Input(()())
Output40
Input()
Output4
NoteLet's consider the first sample test. The bracket sequence from the sample can be colored, for example, as is shown on two figures below.
Given a valid sequence of parentheses, You Need To dye the sequence of parentheses. However, the following conditions must be met: 1. A bracket can be left blank, red, or blue; 2. One pair of matching parentheses can only be colored by one side (the matching here is the only matching, not as long as it is "()", the same below), and must be colored by one side; 3. The colors of adjacent parentheses must be different, but they can be hidden. How many solutions do you have? Because the number of solutions is large, the result model is 1 E9 + 7.
Solution: it is easy to see that this is a DP question and an interval DP question. My DP is very poor. I thought about it for a long time. So I searched for other people's solutions online and suddenly realized it. Set dp [l] [r] [x] [y] to indicate that the color of the left side is x, and that of the right side is y, here, x and y are 0, 1, and 2, respectively, indicating no dyeing, red, or blue. There are three conditions for this interval:
1. l + 1 = r, then they must be a pair of matching brackets. At this time, there may be only four cases where the number of solutions is 1, namely: dp [l] [r] [0] [1] = dp [l] [r] [1] [0] = 1; dp [l] [r] [0] [2] = dp [l] [r] [2] [0] = 1;
2, l and r are a pair of matching brackets, at this time, the interval is divided into two parts, the two ends of the point and the interval [l + 1, r-1], so we can first calculate the number of [l + 1, r-1], and then the State is transferred to the current range, the two ends of the point is also four, can be transferred without conflict, see the code;
3. l and r are not matching parentheses. In this case, the interval can be divided into two parts: interval [l, mid] and interval [mid + 1, r]. here, mid is the matching parentheses corresponding to l. In this way, a valid sequence of parentheses is changed to two valid sequence of parentheses, and the number of solutions is obtained separately, then combine the non-conflicting conditions. For details, see the code.
Attach the AC code:
1 # include <bits/stdc ++. h> 2 using namespace std; 3 typedef long ll; 4 const int maxn = 705; 5 const int mod = 1000000007; 6 ll dp [maxn] [maxn] [3] [3]; 7 string str; 8 stack <int> s; 9 map <int, int> pos; 10 11 void get_match () {12 for (int I = 0; I <str. size (); ++ I) {13 if (str [I] = '(') 14 s. push (I); 15 else {16 pos [I] = s. top (); 17 pos [s. top ()] = I; 18 s. pop (); 19} 20} 21} 22 23 void dfs (int l, int r) {24 If (l + 1 = r) {25 dp [l] [r] [0] [1] = dp [l] [r] [1] [0] = 1; 26 dp [l] [r] [0] [2] = dp [l] [r] [2] [0] = 1; 27 return; 28} 29 if (pos [l] = r) {30 dfs (l + 1, R-1); 31 for (int I = 0; I <3; ++ I) 32 for (int j = 0; j <3; ++ j) {33 if (j! = 1) 34 dp [l] [r] [0] [1] = (dp [l] [r] [0] [1] + dp [l + 1] [r-1] [i] [j]) % mod; 35 if (j! = 2) 36 dp [l] [r] [0] [2] = (dp [l] [r] [0] [2] + dp [l + 1] [r-1] [i] [j]) % mod; 37 if (I! = 1) 38 dp [l] [r] [1] [0] = (dp [l] [r] [1] [0] + dp [l + 1] [r-1] [i] [j]) % mod; 39 if (I! = 2) 40 dp [l] [r] [2] [0] = (dp [l] [r] [2] [0] + dp [l + 1] [r-1] [i] [j]) % mod; 41} 42 return; 43} 44 int mid = pos [l]; 45 dfs (l, mid); 46 dfs (mid + 1, r ); 47 for (int I = 0; I <3; ++ I) 48 for (int j = 0; j <3; ++ j) 49 for (int k = 0; k <3; ++ k) 50 for (int s = 0; s <3; ++ s) 51 if (! (K = 1 & s = 1 )&&! (K = 2 & s = 2 )) 52 dp [l] [r] [I] [j] = (dp [l] [r] [I] [j] + dp [l] [mid] [I] [k] * dp [mid + 1] [r] [s] [j]) % mod; 53} 54 55 int main () {56 ios: sync_with_stdio (false); 57 cin. tie (0); 58 cin> str; 59 get_match (); 60 dfs (0, str. size ()-1); 61 ll ans = 0; 62 for (int I = 0; I <3; ++ I) 63 for (int j = 0; j <3; ++ j) 64 ans = (ans + dp [0] [str. size ()-1] [I] [j]) % mod; 65 cout <ans <endl; 66 return 0; 67}View Code