zoukankan      html  css  js  c++  java
  • 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
    ",ans%Mod);
    61     return 0;
    62 }
    D. Coloring Brackets
    time limit per test 2 seconds
    memory limit per test 256 megabytes
    【Description】
    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).

    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.

    The two ways of coloring shown below are incorrect.

  • 相关阅读:
    Tomcat源码(二):tomcat启动之前的初始化
    Tomcat源码(一):整体架构
    SSH的三个组件ssh、sftp、scp介绍
    远程连接linux服务上的mysql
    linux系统上安装mysql5.6(详细步骤)
    一种基于数字证书的单点登录的实现方法(转)
    jacob操作word (转)
    解决fis3下载慢,没反应
    Spring框架集成mybatis框架的配置(笔记)
    eclipse导入lombok后打不开(如果你的lombok不是最新的,那就来下载最新的)
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/4066605.html
Copyright © 2011-2022 走看看