Brackets

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]
1≤n≤1000000
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
()
 
 
【题意】
  括号匹配(<=10^6),给出已将匹配好的前若干个的括号,让你把剩下的匹配完,问方案数%10^9+7,多组数据<=1000
 
【分析】
  跟上一题类似。先判断他给的括号是否匹配。然后假设左括号比右括号多了l个,就是要求任意前缀和>=l的方案数。也用1与-1互换的方法推
 
  
  上一个厉害的证明:
  
 
  我的理解在上一篇博写了~
 
代码如下:
 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define Maxn 1000010
#define Mod 1000000007
#define LL long long char s[Maxn];
int p[Maxn]; void init()
{
p[]=;
for(int i=;i<=Maxn-;i++)
{
LL x=(LL)p[i-],y=(LL)i,z;
z=(x*y)%Mod;
p[i]=(int)z;
}
} LL qpow(int x,int b)
{
if(x==) return ;
LL xx=x,ans=;
while(b)
{
if(b&) ans=(ans*xx)%Mod;
xx=(xx*xx)%Mod;
b>>=;
}
return ans;
} int get_c(int n,int m)
{
LL ans=p[m];
ans=(ans*qpow(p[n],Mod-))%Mod;
ans=(ans*qpow(p[m-n],Mod-))%Mod;
return (int)ans;
} int main()
{
init();
int n;
while(scanf("%d",&n)!=EOF)
{
int m,sl=;
scanf("%s",s+);
int l=strlen(s+),now=;
bool ok=;
for(int i=;i<=l;i++)
{
if(s[i]=='(') now++,sl++;
else now--;
if(now<) ok=;
}
if(n%!=||l>n||!ok||sl*>n||(l-sl)*>n) {printf("0\n");continue;}
m=n/-sl;
if(sl==n/||l==n) {printf("1\n");continue;}
printf("%d\n",(get_c(m,*m+now)+Mod-get_c(m-,*m+now))%Mod);
}
return ;
}

[HDU 5184]

2016-09-20 19:53:38

【HDU 5184】 Brackets (卡特兰数)的更多相关文章

  1. HDOJ 5184 Brackets 卡特兰数扩展

    既求从点(0,0)仅仅能向上或者向右而且不穿越y=x到达点(a,b)有多少总走法... 有公式: C(a+b,min(a,b))-C(a+b,min(a,b)-1)  /// 折纸法证明卡特兰数: h ...

  2. hdu 5184(数学-卡特兰数)

    Brackets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  3. hdu 5184 类卡特兰数+逆元

    BC # 32 1003 题意:定义了括号的合法排列方式,给出一个排列的前一段,问能组成多少种合法的排列. 这道题和鹏神研究卡特兰数的推导和在这题中的结论式的推导: 首先就是如何理解从题意演变到卡特兰 ...

  4. hdu 5673 Robot 卡特兰数+逆元

    Robot Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  5. hdu 4828 Grids 卡特兰数+逆元

    Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Problem D ...

  6. hdu 4828 Grids(拓展欧几里得+卡特兰数)

    题目链接:hdu 4828 Grids 题目大意:略. 解题思路:将上一行看成是入栈,下一行看成是出栈,那么执着的方案就是卡特兰数,用递推的方式求解. #include <cstdio> ...

  7. 【HDU 5370】 Tree Maker(卡特兰数+dp)

    Tree Maker Problem Description Tree Lover loves trees crazily. One day he invents an interesting gam ...

  8. HDU 4828 (卡特兰数+逆)

    HDU 4828 Grids 思路:能够转化为卡特兰数,先把前n个人标为0.后n个人标为1.然后去全排列,全排列的数列.假设每一个1的前面相应的0大于等于1,那么就是满足的序列,假设把0看成入栈,1看 ...

  9. HDU 6084 寻找母串(卡特兰数)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6084 [题目大意] 对于一个串S,当它同时满足如下条件时,它就是一个01偏串: 1.只由0和1两种 ...

随机推荐

  1. Apache 配置多端口 多虚拟主机 局域网访问

    \wamp\bin\apache\Apache2.4.4\conf\extra\httpd-vhosts.conf 修改如下 NameVirtualHost *:80          Documen ...

  2. LSJ_NHibernate第四章 MVC

    前言: MVC现在已经成为web开发的一个主流趋势了,还没用过的小伙伴,你们已经落伍了,这里我推荐一篇学习博客 玩转Asp.net MVC 的八个扩展点  代码完全开源,下载地址:https://gi ...

  3. 使用ASP.NET实现Windows Service定时执行任务

    转载http://blog.csdn.net/yanghua_kobe/article/details/6937816 我们怎样才能在服务器上使用asp.net定时执行任务而不需要安装windows ...

  4. C#读取Excel表中的数据时,为何有些行的字段内容读取不到

    转载:http://bbs.csdn.net/topics/360220285 1.当某列数据中含有混合类型时,在.NET中使用Microsoft.Jet.OLEDB.4.0来读取Excel文件造成数 ...

  5. 不允许对索引显式地使用 DROP INDEX,该索引正用于 UNIQUE KEY

    [转载]http://blog.csdn.net/w87875251l/article/details/7929657 不允许对索引显式地使用 DROP INDEX,该索引正用于 UNIQUE KEY ...

  6. PHP简单MVC架构

    http://blog.csdn.net/haiqiao_2010/article/details/12166283 由于需要搭建一个简单的框架来进行API接口开发,所以简单的mvc框架当然是首选.最 ...

  7. EasyUI中combotree允许多选的时候onSelect事件会重复触发onCheck事件

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgEAAADkCAIAAACOkmAuAAAgAElEQVR4nO2dW2wc15nnO0EQBJsdzA

  8. Oracle存储过程的理解

    在大专时候学的专业是数据库管理专业,在学校学了各种各样的数据 MSSQL.ORACLE.MySQL. 那时候学数据大部分只学到了些皮毛,仅仅只会按照书上SQL语句,输入计算机得出结果,就很有成就感. ...

  9. redis 在windows上运行

    参考自:https://github.com/ServiceStack/redis-windows 1.用vagrant 运行redis的最后版本 1.1在windows上安装vagrant http ...

  10. 注册一个比较大小Handlebars

    1.显示的数据 var datas = { id:"number" } 2.temp模板 <script id="template" type=" ...