CodeForces 149D Coloring Brackets
传送门:http://codeforces.com/problemset/problem/149/D
很好的dp题,细节甚多
1 #include<set> 2 #include<queue> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 const int N = 750; 10 typedef long long lld; 11 const int Mod=1000000007; 12 #define For(i,n) for(int i=1;i<=n;i++) 13 #define Rep(i,l,r) for(int i=l;i<=r;i++) 14 15 char st[N]; 16 int n,match[N],stack[N]; 17 lld opt[N][N][5][5]; 18 19 bool Check(int l,int r){ 20 if(l==0||r==0||l!=r) return true; 21 return false; 22 } 23 24 lld dp(int l,int r,int c1,int c2){ 25 if(opt[l][r][c1][c2]+1!=0) return opt[l][r][c1][c2]; 26 lld ans=0; 27 if(match[l]==r){ 28 if((c1==0||c2==0)&&(c1+c2)){ 29 if(l+1==r) return opt[l][r][c1][c2]=1; 30 Rep(left,0,2) 31 Rep(right,0,2) 32 if(Check(c1,left)&&Check(right,c2)) 33 ans=(ans+dp(l+1,r-1,left,right))%Mod; 34 } 35 else return opt[l][r][c1][c2]=0; 36 } 37 else{ 38 Rep(left,0,2) 39 Rep(right,0,2) 40 if(Check(left,right)) 41 ans=(ans+dp(l,match[l],c1,left)*dp(match[l]+1,r,right,c2))%Mod; 42 } 43 return opt[l][r][c1][c2]=ans%Mod; 44 } 45 46 void init(){ 47 memset(opt,-1,sizeof(opt)); 48 scanf("%s",st+1); 49 n=strlen(st+1); 50 For(i,n) 51 if(st[i]=='(') stack[++stack[0]]=i; 52 else match[stack[stack[0]--]]=i; 53 } 54 55 int main(){ 56 init();lld ans=0; 57 Rep(i,0,2) 58 Rep(j,0,2) 59 ans=(ans+dp(1,n,i,j))%Mod; 60 printf("%d\n",ans%Mod); 61 return 0; 62 }
You are given string s. 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 fifth bracket corresponds to the fourth one.
You are allowed to color some brackets in the bracket sequence so as all three conditions are fulfilled:
- Each bracket is either not colored any color, or is colored red, or is colored blue.
- For any pair of matching brackets exactly one of them is colored. In other words, for any bracket the following is true: either it or the matching bracket that corresponds to it is colored.
- No two neighboring colored brackets have the same color.
Find the number of different ways to color the bracket sequence. The ways should meet the above-given conditions. Two ways of coloring are considered different if they differ in the color of at least one bracket. As the result can be quite large, print it modulo 1000000007 (109 + 7).
The first line contains the single string s (2 ≤ |s| ≤ 700) which represents a correct bracket sequence.
Print the only number — the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007 (109 + 7).
(())
12
(()())
40
()
4
Let's consider the first sample test. The bracket sequence from the sample can be colored, for example, as is shown on two figures below.
The two ways of coloring shown below are incorrect.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 《HelloGitHub》第 106 期
· 数据库服务器 SQL Server 版本升级公告
· 深入理解Mybatis分库分表执行原理
· 使用 Dify + LLM 构建精确任务处理应用