zoukankan      html  css  js  c++  java
  • hdu 5184(数学-卡特兰数)

    Brackets

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 659    Accepted Submission(s): 170


    Problem Description
    We give the following inductive definition of a “regular brackets” sequence:
    ● the empty sequence is a regular brackets sequence,
    ● if s is a regular brackets sequence, then (s) are regular brackets sequences, and
    ● if a and b are regular brackets sequences, then ab is a regular brackets sequence.
    ● no other sequence is a regular brackets sequence

    For instance, all of the following character sequences are regular brackets sequences:
    (), (()), ()(), ()(())
    while the following character sequences are not:
    (, ), )(, ((), ((()

    Now we want to construct a regular brackets sequence of length n, how many regular brackets sequences we can get when the front several brackets are given already.
     
    Input
    Multi test cases (about 2000), every case occupies two lines.
    The first line contains an integer n.
    Then second line contains a string str which indicates the front several brackets.

    Please process to the end of file.

    [Technical Specification]
    1n1000000
    str contains only '(' and ')' and length of str is larger than 0 and no more than n.
     
    Output
    For each case,output answer % 1000000007 in a single line.
     
    Sample Input
    4 () 4 ( 6 ()
     
    Sample Output
    1 2 2
    Hint
    For the first case the only regular sequence is ()(). For the second case regular sequences are (()) and ()(). For the third case regular sequences are ()()() and ()(()).
     
    Source
    卡特兰数经典模型:
     
    题意:给出一个数字 n ,然后会有 n 个 '(' 和 ')' 组成一个字符串序列,这个字符串序列的 ( 和 ) 是相互匹配的,现在给出这个字符串序列的前几项,要你判断符合前缀为给出序列的字符串会有多少种??
    题解:如果没有前缀条件就是裸的卡特兰数的模型.如果已经确定了前缀,当前缀满足的情况下,对于后面的串这个时候 '(' 的数量不会会大于等于 ')' 的数量,所以我们将其看成一个经典的买票模型.
    n+m个人排队买票,并且满足,票价为50元,其中n个人各手持一张50元钞票,m个人各手持一张100元钞票,除此之外大家身上没有任何其他的钱币,并且初始时候售票窗口没有钱,问有多少种排队的情况数能够让大家都买到票。
    这里的话将 ')' 看成50 的,'(' 看成100的,只要当前的 ')'比 '(' 多我们总是可以找到一个序列.所以这里还是一个卡特兰数模型。。
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    #include <math.h>
    using namespace std;
    typedef long long LL;
    const LL mod = 1000000007;
    const int N = 1000005;
    char s[N];
    LL f[N];
    LL pow_mod(LL a,LL n){
        LL ans = 1;
        while(n){
            if(n&1) ans = ans*a%mod;
            a = a*a%mod;
            n>>=1;
        }
        return ans;
    }
    void init(){
        f[0] = f[1] = 1;
        for(int i=2;i<N;i++){
            f[i] = f[i-1]*i%mod;
        }
    }
    LL C(LL n,LL m){
        LL a = f[m]*f[n-m]%mod;
        LL inv = pow_mod(a,mod-2);
        return f[n]*inv%mod;
    }
    int main()
    {
        init();
        int n;
        while(scanf("%d",&n)!=EOF){
            scanf("%s",&s);
            if(n%2==1){
                printf("0
    ");
                continue;
            }
            int len = strlen(s);
            int l=0,r=0;
            bool flag = true;
            for(int i=0;i<len;i++){ ///已经加入的左括号必须不小于右括号
                if(s[i]=='(') l++;
                if(s[i]==')') r++;
                if(l<r) {
                    flag = false;
                    break;
                }
            }
            if(!flag||l<r){
                printf("0
    ");
                continue;
            }
            int m= n/2;
            l = m-l,r = m-r;
            if(l<0||r<0){  ///防止这种情况 4 ((()
                printf("0
    ");
                continue;
            }
            printf("%lld
    ",(C(l+r,r)-C(l+r,r+1)+mod)%mod);
        }
        return 0;
    }
  • 相关阅读:
    Jenkins遇到问题一:jenkins配置权限不对导致无法登陆或者空白页面解决办法
    翻页功能的测试用例
    Jenkins学习二:Jenkins安装与配置
    Jenkins学习一:Jenkins是什么?
    Java MyEclipse下Ant build.xml简单实例详解
    Linux环境中Openfire安装指南
    Linux下查看文件和文件夹大小
    Tsung测试openfire服务器
    Windows环境中Openfire与Spark安装与配置指南
    解决-bash: lsb_release: command not found
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5690779.html
Copyright © 2011-2022 走看看