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]
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
()
 
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 = ;
const int N = ;
char s[N];
LL f[N];
LL pow_mod(LL a,LL n){
LL ans = ;
while(n){
if(n&) ans = ans*a%mod;
a = a*a%mod;
n>>=;
}
return ans;
}
void init(){
f[] = f[] = ;
for(int i=;i<N;i++){
f[i] = f[i-]*i%mod;
}
}
LL C(LL n,LL m){
LL a = f[m]*f[n-m]%mod;
LL inv = pow_mod(a,mod-);
return f[n]*inv%mod;
}
int main()
{
init();
int n;
while(scanf("%d",&n)!=EOF){
scanf("%s",&s);
if(n%==){
printf("0\n");
continue;
}
int len = strlen(s);
int l=,r=;
bool flag = true;
for(int i=;i<len;i++){ ///已经加入的左括号必须不小于右括号
if(s[i]=='(') l++;
if(s[i]==')') r++;
if(l<r) {
flag = false;
break;
}
}
if(!flag||l<r){
printf("0\n");
continue;
}
int m= n/;
l = m-l,r = m-r;
if(l<||r<){ ///防止这种情况 4 ((()
printf("0\n");
continue;
}
printf("%lld\n",(C(l+r,r)-C(l+r,r+)+mod)%mod);
}
return ;
}

最新文章

  1. [NOIP2014]联合权值 题解
  2. shudupoj2676
  3. css3 animation动画特效插件的巧用
  4. eclipse下使用git下载和上传项目
  5. Android 下拉刷新框架实现
  6. qml json 解析到 ListView
  7. IOS之表视图添加搜索栏
  8. android AsyncTask异步下载并更新进度条
  9. bzoj2428: [HAOI2006]均分数据
  10. pymongo 例子
  11. IOS开发之程序执行状态更改
  12. System.BadImageFormatException: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
  13. 如何优化运行在webkit上的web app
  14. 云计算+SaaS+业务开发平台=JSAAS云平台
  15. C# 添加Excel表单控件(Form Controls)
  16. mysql使用错误
  17. 25.Hibernate-配置文件.md
  18. sqlserver搜索中怎么把varchar类型转换成numeric类型
  19. C#右下角弹出消息框
  20. iOS开发-按钮的基本使用

热门文章

  1. C#数据库连接问题
  2. 获取JavaScript对象的方法
  3. 正则awk和查看文件行数
  4. 【bzoj3940】[Usaco2015 Feb]Censoring AC自动机
  5. BZOJ4544 椭圆上的整点(数论)
  6. P2671 求和
  7. 【题解】SDOI2018战略游戏
  8. React生命周期总结
  9. NEYC 2017 游记
  10. vector创建2维数组