【CodeForces 149D】 【dp+dfs好题】D. Coloring Brackets【在限制条件下括号染色问题】

传送门:D. Coloring Brackets

描述:

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
(())
output
12
input
(()())
output
40
input
()
output
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.

题意:

给出一组合法的括号。

括号要么不涂颜色,要么就涂上红色或者绿色。

匹配的括号只能有一个有颜色。

两个相邻(注意这个条件)的括号不能有相同的颜色。


思路:

条件中有相邻,一下就可以想到可能要用区间DP

这题不同于POJ2955这道经典题,题解见Here

这里的合法括号序列只有小括号。

所以立马可以想到每个括号与之匹配的位置是一定的,就先预处理一下匹配的括号。

接着就可以将这个序列分成两个区间。 (L - match[L] )  (match[L]+1, R)

用递归先处理小区间,再转移大区间。


这题刚开始想半天没什么头绪是因为条件的限制,所以记录区间的同时,还要记录区间端点的颜色。

dp[l][r][lc][rc]表示l~r的括号串,最左边涂lc,最右边涂rc

然后就是一个递归的过程。


dp的时候,当左右两边括号是匹配的时候,就为两个括号染色,然后就把状态从它们里面的括号串转移过来

当不匹配的时候,也就是这个串是由多个匹配串并列而成的,那么就是dfs的思想了,为第一对括号染色,并求这对括号的子括号串的染色方案数,然后dp后面的那些串,直到dp到只剩一个串,并根据乘法原则把各种方案累计起来。(这样做是因为染色是和两边的括号颜色有关系的,而我们在dp前必须确定这个串两端的括号颜色。

递归的停止是当括号只有两个时,返回可能的染色数。


写这题只要思路清晰就能模拟出来整个dfs过程


代码:

#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << "  " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
using  namespace  std;
const ll mod=1e9+7;

ll dp[800][800][3][3];
int stk[800];
int match[800];
char s[800];
int top=-1;

void dfs(int l,int r){
  if(l+1==r){
    dp[l][r][0][1]=dp[l][r][0][2]=dp[l][r][1][0]=dp[l][r][2][0]=1;
    return ;
  }
  if(match[l]==r){
    dfs(l+1, r-1);

    for(int i=0; i<3; i++){
      for(int j=0; j<3; j++){
        if(j!=1)dp[l][r][0][1]=(dp[l][r][0][1]+dp[l+1][r-1][i][j])%mod;//内括号涂色j不是1时,转移到外括号是1的数量,下同
        if(j!=2)dp[l][r][0][2]=(dp[l][r][0][2]+dp[l+1][r-1][i][j])%mod;
        if(i!=1)dp[l][r][1][0]=(dp[l][r][1][0]+dp[l+1][r-1][i][j])%mod;
        if(i!=2)dp[l][r][2][0]=(dp[l][r][2][0]+dp[l+1][r-1][i][j])%mod;
      }
    }
  }
  else{
    dfs(l, match[l]);
    dfs(match[l]+1, r);

    for(int i=0; i<3; i++)
      for(int j=0; j<3; j++)
        for(int k=0; k<3; k++)
          for(int p=0; p<3; p++){
            if(j && j==k)continue;
            dp[l][r][i][p]=(dp[l][r][i][p]+(dp[l][match[l]][i][j]*dp[match[l]+1][r][k][p]%mod))%mod;
          }
  }
}

int  main(){
  #ifndef ONLINE_JUDGE
  freopen("in.txt","r",stdin);
  #endif

  scanf("%s",s+1);
  int n=strlen(s+1);
  memset(match, 0,sizeof(match));
  memset(dp, 0,sizeof(dp));

  for(int i=1; i<=n; i++){
    if(s[i]=='(')stk[++top]=i;
    else match[stk[top--]]=i;
  }

  dfs(1, n);
  ll ans=0;
  for(int i=0; i<3; i++)
    for(int j=0; j<3; j++)
      ans+=dp[1][n][i][j];
  printf("%I64d\n",ans%mod);
  return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值