C. Famil Door and Brackets

题目连接:

http://www.codeforces.com/contest/629/problem/C

Description

As Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length n more than any other strings!

The sequence of round brackets is called valid if and only if:

the total number of opening brackets is equal to the total number of closing brackets;

for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.

Gabi bought a string s of length m (m ≤ n) and want to complete it to obtain a valid sequence of brackets of length n. He is going to pick some strings p and q consisting of round brackets and merge them in a string p + s + q, that is add the string p at the beginning of the string s and string q at the end of the string s.

Now he wonders, how many pairs of strings p and q exists, such that the string p + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109 + 7.

Input

First line contains n and m (1 ≤ m ≤ n ≤ 100 000, n - m ≤ 2000) — the desired length of the string and the length of the string bought by Gabi, respectively.

The second line contains string s of length m consisting of characters '(' and ')' only.

Output

Print the number of pairs of string p and q such that p + s + q is a valid sequence of round brackets modulo 109 + 7.

Sample Input

4 1

(

Sample Output

4

Hint

题意

给你一个长度m的括号序列,然后让你补全成长度为n的合法括号序列

问你一共有多少种补全的方案

题解:

dp

dp[i][j]表示长度为i,当前平衡度为j的方案数是多少

然后暴力枚举ij就好了,左右乘法原则处理一下贡献

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2005;
const int mod = 1e9+7;
long long dp[maxn][maxn];//长度为i,平衡度为j的方案数
int n,m;
char s[100005];
int main()
{
dp[0][0]=1;
for(int i=1;i<=2000;i++)
{
dp[i][0]=dp[i-1][1];
for(int j=1;j<=i;j++)
dp[i][j]=(dp[i-1][j-1]+dp[i-1][j+1])%mod;
}
scanf("%d%d",&n,&m);
scanf("%s",s+1);
int sum=0,l=0,r=0;
for(int i=1;i<=m;i++)
{
if(s[i]=='(')sum++;
else sum--;
l=min(sum,l);
}
l=-l;
sum=0;
for(int i=m;i;i--)
{
if(s[i]==')')sum++;
else sum--;
r=min(sum,r);
}
r=-r;
long long ans = 0;
for(int i=0;i<=n-m;i++)
{
for(int j=0;j<=n-m;j++)
{
int len2 = n-m-i;
int j2 = j-sum;
if(j2<=n-m&&i>=l&&j2>=r)
ans=(ans+dp[i][j]*dp[len2][j2])%mod;
}
}
cout<<ans<<endl;
}

Codeforces Round #343 (Div. 2) C. Famil Door and Brackets dp的更多相关文章

  1. Codeforces Round #343 (Div. 2) C. Famil Door and Brackets

    题目链接: http://codeforces.com/contest/629/problem/C 题意: 长度为n的括号,已经知道的部分的长度为m,现在其前面和后面补充‘(',或')',使得其长度为 ...

  2. Codeforces Round #343 (Div. 2) E. Famil Door and Roads lca 树形dp

    E. Famil Door and Roads 题目连接: http://www.codeforces.com/contest/629/problem/E Description Famil Door ...

  3. Codeforces Round #343 (Div. 2) E. Famil Door and Roads

    题目链接: http://www.codeforces.com/contest/629/problem/E 题解: 树形dp. siz[x]为x这颗子树的节点个数(包括x自己) dep[x]表示x这个 ...

  4. Codeforces Round #343 (Div. 2) E. Famil Door and Roads (树形dp,lca)

    Famil Door's City map looks like a tree (undirected connected acyclic graph) so other people call it ...

  5. Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

    Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...

  6. Codeforces Round #343 (Div. 2)

    居然补完了 组合 A - Far Relative’s Birthday Cake import java.util.*; import java.io.*; public class Main { ...

  7. Codeforces Round #343 (Div. 2) B. Far Relative’s Problem 暴力

    B. Far Relative's Problem 题目连接: http://www.codeforces.com/contest/629/problem/B Description Famil Do ...

  8. Codeforces Round #343 (Div. 2) B

    B. Far Relative’s Problem time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  9. Codeforces Round #343 (Div. 2) A. Far Relative’s Birthday Cake 水题

    A. Far Relative's Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/A Description Do ...

随机推荐

  1. 为什么Windows7打开项目的方式是灰的不能修改

    http://jingyan.baidu.com/article/d3b74d64a964691f77e60900.html 进入组策略编辑器,即运行gpedit.msc,进入“用户配置”-“管理模板 ...

  2. linux内核网络接收数据流程图【转】

    转自:http://blog.chinaunix.net/uid-23069658-id-3141409.html 4.3 数据接收流程图   各层主要函数以及位置功能说明:          1)s ...

  3. docker配置桥接网络

    [root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# cp ifcfg-et ...

  4. php常见术语

    什么是PHP? php是Hypertext Preprocessor的缩写,php是一种内嵌 HTML的脚本语言.PHP的独特语法混合了c,java和perl及PHP式的新语法.这门语言的的目标是让网 ...

  5. mui框架 页面无法滚动解决方法

    只需要初始化一下就可以了 mui.init(); 加下面这段代码即可: (function($){ $(".mui-scroll-wrapper").scroll({ //boun ...

  6. MYSQL表中设置字段类型为TIMESTAMP时的注意事项

    在MYSQL中,TIMESTAMP类型是用来表示日期的,但是和DATETIME不同,不同点就不再这里说明了. 当我们在使用TIMESTAMP类型设置表中的字段时,我们应该要注意一点,首先我们在表中新增 ...

  7. 几个例子理解对称加密与非对称加密、公钥与私钥、签名与验签、数字证书、HTTPS加密方式

    # 原创,转载请留言联系 为什么会出现这么多加密啊,公钥私钥啊,签名啊这些东西呢?说到底还是保证双方通信的安全性与完整性.例如小明发一封表白邮件给小红,他总不希望给别人看见吧.而各种各样的技术就是为了 ...

  8. linux命令(33):tail命令

    1.命令格式; tail[必要参数][选择参数][文件] 2.命令功能: 用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理.常用查看日志文件. 3.命令参数: -f 循环读取 -q 不显示 ...

  9. Nginx惊群处理

    惊群:是指在多线程/多进程中,当有一个客户端发生链接请求时,多线程/多进程都被唤醒,然后只仅仅有一个进程/线程处理成功,其他进程/线程还是回到睡眠状态,这种现象就是惊群. 惊群是经常发生现在serve ...

  10. ZOJ-3318

    Strange Country Time Limit: 1 Second      Memory Limit: 32768 KB There are n cities in the dream cou ...