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

    区间DP。。。。。。

    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.

    CodeForces 149D  Coloring Brackets - qhn999 - 码代码的猿猿

    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).

    Sample test(s)
    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.

    CodeForces 149D  Coloring Brackets - qhn999 - 码代码的猿猿CodeForces 149D  Coloring Brackets - qhn999 - 码代码的猿猿

    The two ways of coloring shown below are incorrect.

    CodeForces 149D  Coloring Brackets - qhn999 - 码代码的猿猿CodeForces 149D  Coloring Brackets - qhn999 - 码代码的猿猿


    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <string>
    #include <stack>

    using namespace std;

    typedef long long int LL;

    const int MOD=1000000007;

    int ma[1000];
    int dp[1000][1000][3][3];
    string str;

    bool check(int a,int b)
    {
        if(a==0||b==0||a!=b) return true;
        else return false;
    }

    void init(string str)
    {
        stack<int> st;
        memset(ma,-1,sizeof(ma));
        memset(dp,-1,sizeof(dp));

        int len=str.size();
        for(int i=0;i<len;i++)
        {
           if(str=='(')
           {
               st.push(i);
           }
           else if(str==')')
           {
               ma[st.top()]=i;
               st.pop();
           }
        }
    }

    LL solve(int l,int r,int c1,int c2)
    {
        if(dp[l]
    [c1][c2]!=-1)
            return dp[l]
    [c1][c2];
        LL res=0;int i,j;

        if(r==ma[l])
        {
            if((c1==0)^(c2==0))
            {
                if(l+1==r)
                {
                    res=dp[l]
    [c1][c2]=1;
                    return res;
                }
                for(i=0;i<3;i++)
                {
                    for(j=0;j<3;j++)
                    {
                        if(check(c1,i)&&check(j,c2))
                        {
                            res=(res%MOD+solve(l+1,r-1,i,j)%MOD)%MOD;
                        }
                    }
                }
            }
            else
            {
                res=dp[l]
    [c1][c2]=0;
                return res;
            }
        }
        else
        {
            for(i=0;i<3;i++)
            {
                for(j=0;j<3;j++)
                {
                    if(check(i,j))
                    {
                        res=(res+(solve(l,ma[l],c1,i)%MOD*solve(ma[l]+1,r,j,c2)%MOD)%MOD)%MOD;
                    }
                }
            }
        }
        dp[l]
    [c1][c2]=res%MOD;
        return res%MOD;
    }

    int main()
    {
        string s;
        cin>>s;
        init(s);
        LL ans=0;
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                ans+=solve(0,s.size()-1,i,j);
            }
        }
        cout<<ans%MOD<<endl;
        return 0;
    }

  • 相关阅读:
    struts2_文件上传和下载
    struts2_方法拦截器
    struts2_Action之间的重定向传参
    struts2_全局的拦截器,拦截用户非法登陆
    Hibernate入门心得
    struts2_异常页面处理
    设计师小法器:字体大管家
    IE6,IE7下设置body{overflow:hidden;}失效Bug【转】
    子层的margintop属性应用到父层上的解决方法
    jQuery CSS选择器nthchild
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350946.html
Copyright © 2011-2022 走看看