Test instructions
Give a valid parenthesis sequence that contains only () these two types. Now to paint one of the parentheses in each pair of parentheses, there are two choices: blue or red. Ask not to have two of the same color parentheses next to each other, how many kinds of staining methods?
Ideas:
It's a lot more analog, isn't it? Two colors and colorless, with 2 bit can be expressed. Then there are all kinds of transfers, note that the results may be very large, to be output after modulo. The shift is mainly not to let the same color parentheses together. You can use DFS in the process to find all the legal parentheses farthest from the interval [l,r], recursively down the sub-problem until the remaining pair of parentheses are processed directly.
1 //#include <bits/stdc++.h>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <map>7#include <algorithm>8#include <vector>9#include <iostream>Ten #definePII pair<int,int> One #defineINF 0x3f3f3f3f A #defineLL unsigned long Long - using namespacestd; - Const DoublePI = ACOs (-1.0); the Const intn= +; - Const intmod=1e9+7; - CharS[n]; - +LL dp[n][n][3][3];//0 colorless, 1 red, 2 blue - + voidCalintllintMidintRr//Interval Merging A { at if(Ll==mid)return ; - for(intI=0; i<3; i++) - for(intj=0; j<3; J + +) - for(intk=0; k<3; k++) - for(intu=0; u<3; u++) - if(k!=u| | k==0) indp[ll][rr][i][j]+= (dp[ll][mid-1][I][K] * Dp[mid][rr][u][j])%MoD; - } to voidCal2 (intllintRr//interval Nesting + { - for(intk=0; k<3; k++)//enumeration Inner Interval the for(intu=0; u<3; u++) * { $ if(u!=1) dp[ll][rr][0][1]= (dp[ll][rr][0][1]+dp[ll+1][rr-1][k][u])%MoD;Panax Notoginseng if(u!=2) dp[ll][rr][0][2]= (dp[ll][rr][0][2]+dp[ll+1][rr-1][k][u])%MoD; - if(k!=1) dp[ll][rr][1][0]= (dp[ll][rr][1][0]+dp[ll+1][rr-1][k][u])%MoD; the if(k!=2) dp[ll][rr][2][0]= (dp[ll][rr][2][0]+dp[ll+1][rr-1][k][u])%MoD; + } A } the + voidDFS (intLintR) - { $ if(L +1==R) $ { -dp[l][r][1][0]=dp[l][r][0][1]=1; -dp[l][r][2][0]=dp[l][r][0][2]=1; the return ; - }Wuyi intq=l+1; the while(Q<r)//dividing [L->r] into multiple sub-ranges - { Wu intP=q, cnt=0; - while(1)//find the corresponding other half bracket) position About { $ if(s[q]=='(') cnt++; - if(s[q]==')') cnt--; - if(cnt==0) Break; -q++; A } +DFS (P, q);//calculation interval [p->q] theCal (L +1, p, Q);//two interval merging -q++; $ } theCal2 (L,R);//sub-range and to large interval the } the the - intMain () in { the //freopen ("Input.txt", "R", stdin); theMemset (DP,0,sizeof(DP)); Aboutscanf"%s", s+1); the intN=strlen (s+1); theDFS (0, n+1);//Treat s[0] and s[n+1] as a pair of parentheses. theLL ans=0; + - for(intI=0; i<3; i++) the for(intj=0; j<3; J + +)BayiAns= (ans+dp[1][N][I][J])%MoD; the thecout<<ans<<Endl; - return 0; -}
AC Code
Codeforces 149D Coloring Brackets (interval D)