题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数


区间DP

用栈先处理匹配

f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数

l和r匹配的话,转移到(l+1,r-1)

不匹配,i的匹配p一定在l和r之间,从p分开转移

听说用记忆化搜索比较快,可以像树形DP那样写记忆化搜索,也可以传统的四个参数那样写

用循环+条件判断,简化状态转移的枚举

注意细节 见代码


  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. const int N=,MOD=1e9+;
  7. char s[N];
  8. long long n,f[N][N][][];
  9. int st[N],top=,m[N];
  10. void match(){
  11. for(int i=;i<=n;i++){
  12. if(s[i]=='(') st[++top]=i;
  13. else{
  14. int tmp=st[top--];
  15. m[i]=tmp;
  16. m[tmp]=i;
  17. }
  18. }
  19. }
  20. void dp(int l,int r){//printf("dp %d %d\n",l,r);
  21. if(l>=r) return;
  22. if(l+==r){
  23. f[l][r][][]=f[l][r][][]=f[l][r][][]=f[l][r][][]=;
  24. return;
  25. }
  26. if(m[l]==r){
  27. dp(l+,r-);
  28. for(int i=;i<;i++)
  29. for(int j=;j<;j++){
  30. if(j!=) f[l][r][][]=(f[l][r][][]+f[l+][r-][i][j])%MOD;
  31. if(j!=) f[l][r][][]=(f[l][r][][]+f[l+][r-][i][j])%MOD;
  32. if(i!=) f[l][r][][]=(f[l][r][][]+f[l+][r-][i][j])%MOD;
  33. if(i!=) f[l][r][][]=(f[l][r][][]+f[l+][r-][i][j])%MOD;
  34. }
  35. }else{
  36. int p=m[l];
  37. dp(l,p);dp(p+,r);
  38. for(int i=;i<;i++)
  39. for(int j=;j<;j++)
  40. for(int k=;k<;k++)
  41. for(int t=;t<;t++){
  42. if(k==&&t==) continue;
  43. if(k==&&t==) continue;
  44. //if(i!=0&&t!=0) continue; 不需要,因为已保证这样的话值是0
  45. f[l][r][i][j]=(f[l][r][i][j]+f[l][p][i][k]*f[p+][r][t][j]%MOD)%MOD;
  46. }
  47. }
  48. //printf("%d %d %d %d %d %d\n",l,r,f[l][r][0][1],f[l][r][0][2],f[l][r][1][0],f[l][r][2][0]);
  49. }
  50. //void dp(int l,int r,int a,int b){
  51. // int &ans=f[l][r][a][b];
  52. // if(ans!=-1) return ans;
  53. //
  54. //}
  55. int main(){
  56. scanf("%s",s+);
  57. n=strlen(s+);
  58. match();
  59. dp(,n);
  60. long long ans=;
  61. for(int i=;i<;i++)
  62. for(int j=;j<;j++)
  63. ans=(ans+f[][n][i][j])%MOD;
  64.  
  65. printf("%d",ans);
  66. }
D. Coloring Brackets
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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 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 modulo1000000007 (109 + 7).

Input

The first line contains the single string s (2 ≤ |s| ≤ 700) which represents a correct bracket sequence.

Output

Print the only number — the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007(109 + 7).

Examples
input
  1. (())
output
  1. 12
input
  1. (()())
output
  1. 40
input
  1. ()
output
  1. 4
Note

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.



CF149D. Coloring Brackets[区间DP !]的更多相关文章

  1. Codeforces Round #106 (Div. 2) D. Coloring Brackets —— 区间DP

    题目链接:https://vjudge.net/problem/CodeForces-149D D. Coloring Brackets time limit per test 2 seconds m ...

  2. codeforces 149D Coloring Brackets (区间DP + dfs)

    题目链接: codeforces 149D Coloring Brackets 题目描述: 给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的. 1,每个括号只有三种选择:涂红 ...

  3. Codeforces Round #106 (Div. 2) D. Coloring Brackets 区间dp

    题目链接: http://codeforces.com/problemset/problem/149/D D. Coloring Brackets time limit per test2 secon ...

  4. CF 149D Coloring Brackets 区间dp ****

    给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2.每对括号必须只能给其中的一个上色 3.相邻的两个不能上同色,可以都不上色 求0-len-1这一区间内 ...

  5. Codeforces149D - Coloring Brackets(区间DP)

    题目大意 要求你对一个合法的括号序列进行染色,并且需要满足以下条件 1.要么不染色,要么染红色或者蓝色 2.对于任何一对括号,他们当中有且仅有一个被染色 3.相邻的括号不能染相同的颜色 题解 用区间d ...

  6. codeforce 149D Coloring Brackets 区间DP

    题目链接:http://codeforces.com/problemset/problem/149/D 继续区间DP啊.... 思路: 定义dp[l][r][c1][c2]表示对于区间(l,r)来说, ...

  7. CodeForces 149D Coloring Brackets 区间DP

    http://codeforces.com/problemset/problem/149/D 题意: 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2 ...

  8. CF149D Coloring Brackets

    CF149D Coloring Brackets Link 题面: 给出一个配对的括号序列(如"\((())()\)"."\(()\)"等, "\() ...

  9. Codeforces 508E Arthur and Brackets 区间dp

    Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...

随机推荐

  1. Web 开发中应用 HTML5 技术的10个实例教程

    HTML5 作为下一代网站开发技术,无论你是一个 Web 开发人员或者想探索新的平台的游戏开发者,都值得去研究.借助尖端功能,技术和 API,HTML5 允许你创建响应性.创新性.互动性以及令人惊叹的 ...

  2. jQuery仿京东无限级菜单HoverTree

    官方网址:http://keleyi.com/jq/hovertree/ 效果图: 看了上面效果图,你或许已经明白为什么是仿京东菜单.如果还不明白,请访问http://list.jd.com/list ...

  3. scroll事件实现监控滚动条并分页显示示例(zepto.js)

    scroll事件实现监控滚动条并分页显示示例(zepto.js  ) 需求:在APP落地页上的底部位置显示此前其他用户的购买记录,要求此div盒子只显示3条半,但一页有10条,div内的滑动条滑到一页 ...

  4. Tomcat部署记事

    1.导入证书到jdk里 keytool -import -alias 证书名称 -file 证书地址 -keystore 导入位置 例:keytool -import -alias co3 -file ...

  5. 对比MS Test与NUnit Test框架

    前言: 项目中进行Unit Test时,肯定会用到框架,因为这样能够更快捷.方便的进行测试. .Net环境下的测试框架非常多,在这里只是对MS Test和NUnit Test进行一下比较, 因为这两个 ...

  6. chrome developer tool—— 断点调试篇

    断点,调试器的功能之一,可以让程序中断在需要的地方,从而方便其分析.也可以在一次调试中设置断点,下一次只需让程序自动运行到设置断点位置,便可在上次设置断点的位置中断下来,极大的方便了操作,同时节省了时 ...

  7. 给栅格数据添加RasterFunction--自定义渲染方法

    <script type="text/javascript"> /** dojo.require("esri.map"); dojo.require ...

  8. github-ssh

        # lsb_release -a    No LSB modules are available.    Distributor ID:    Ubuntu    Description:   ...

  9. Android Handler机制(二)---MessageQueue源码解析

    MessageQueue 1.变量 private final boolean mQuitAllowed;//表示MessageQueue是否允许退出 @SuppressWarnings(" ...

  10. 优化MySchool数据库(存储过程)

    什么是“存储过程”: ---- 数据库中,用于存储“业务逻辑”的技术!(T-SQL代码当做数据一样保存到数据可) 语法 : [if exists(select * from sysobjects wh ...