zoukankan      html  css  js  c++  java
  • CodeForces 149D Coloring Brackets

    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 modulo 1000000007 (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.

    题目大意:给定一个合法的括号序列,现在要你给括号序列染色。但是必须要满足如下条件:一、一个括号可以不染色,或者染红色,或者染蓝色;二、一对匹配的括号只能有一边染色(这里的匹配是唯一的对应,而不是只要是"()"就可,下同),且必须有一边染色;三、相邻的括号染的颜色必须不一样,但是可以都不染色。问你有多少种方案?因为方案数很大,所以结果模去1e9+7。

     

    解题思路:容易看出这是一道DP题,并且是一道区间DP题。自己的DP很差,想了半天没想出来。于是去网上搜了一下别人的解法,瞬间恍然大悟了。设dp[l][r][x][y]表示区间[l,r]左端染的色是x,右端染的色是y的方案数,其中x,y取0,1,2,分别表示不染色,染红色,染蓝色。则该区间有三种情况,如下:

    1、l+1==r,那么它们一定就是一对匹配的括号,此时,只可能有四种情况,方案数均为1,即:dp[l][r][0][1] = dp[l][r][1][0] = 1;dp[l][r][0][2] = dp[l][r][2][0] = 1;

    2、l和r是一对匹配的括号,此时,区间被分为两部分,两端点以及区间[l+1,r-1],那么我们可以先算出区间[l+1,r-1]的方案数,再由此状态转移到当前区间,两端点情况也就四种,不冲突即可转移,详见代码;

    3、l和r不是一对匹配的括号,此时,区间也可被分成两部分,区间[l,mid]和区间[mid+1,r],其中mid为l所对应与之匹配的括号,这样,一个合法的括号序列变成两个合法的括号序列,将它们分别求出方案数,再将不冲突的情况组合起来即可,详见代码。

     

    附上AC代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long 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
    本文为博主原创文章,转载请注明出处:http://www.cnblogs.com/Silenceneo-xw/
  • 相关阅读:
    高并发的epoll+线程池,epoll在线程池内ehyyngpChinaUnix博客
    用vim处理字符的大小写转换
    jabberd14 XMPP/Jabber server daemon
    thriftmissingguide/
    高并发的epoll+多线程ehyyngpChinaUnix博客
    大讲堂
    [基于Epoll内置LeaderFollower服务端实现, 已可达50万echo qps(全新支持Lua啦)] C/C++ ChinaUnix.net
    来说说epoll+线程池
    boost高并发网络框架+线程池ehyyngpChinaUnix博客
    高并发的epoll+线程池,业务在线程池内ehyyngpChinaUnix博客
  • 原文地址:https://www.cnblogs.com/Silenceneo-xw/p/5936579.html
Copyright © 2011-2022 走看看