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:

  1. the total number of opening brackets is equal to the total number of closing brackets;
  2. 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.

Examples
input
4 1
(
output
4
input
4 4
(())
output
1
input
4 3
(((
output

0

题意:给你一个长度为n的括号匹配串(不一定恰好匹配),让你在这个串的前面和后面加上一些括号匹配串,使得这个括号串平衡(平衡的含义是对于任意位置的括号前缀和大于等于0,且最后的前缀和为0)。

思路:比较容易想到的思路是枚举这个字符串前面p字符串的长度,那么后面q字符串的长度就知道了。那么p字符串要满足什么条件呢,因为要使得任意位置的前缀和大于等于0,所以我们可以使得p字符串的前缀和大于等于字符串s的最小前缀和minx,那么p+s就符合前缀和大于等于0,然后q的方案数也能确定了。我们用dp[i][j]表示i个括号平衡度为j的方案数,那么可以先预处理出来dp的值。然后我们算出s字符串的最小前缀和minx,最后我们只要枚举p的长度和平衡度c,那么sum+=dp[p][c]*dp[n-m-p][now+c],(now是整个s字符串的平衡度,考虑q的方案数时,我们要考虑对称性)。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define eps 1e-15
#define maxn 100050
#define MOD 1000000007
char s[maxn];
ll dp[2050][2060]; int main()
{
int n,m,i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
scanf("%s",s+1);
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(i=1;i<=n-m;i++){
for(j=0;j<=i;j++){
if(j>0){
dp[i][j]=(dp[i][j]+dp[i-1][j-1])%MOD;
}
dp[i][j]=(dp[i][j]+dp[i-1][j+1])%MOD;
if(dp[i][j]>=MOD)dp[i][j]-=MOD;
}
}
int minx=inf;
int now=0;
for(i=1;i<=m;i++){
if(s[i]=='(')now++;
else now--;
minx=min(minx,now);
} ll sum=0;
for(i=0;i<=n-m;i++){
for(j=0;j<=i;j++){
if(j>=-minx && j+now<=n-m-i){
sum=(sum+dp[i][j]*dp[n-m-i][j+now ])%MOD;
}
}
}
printf("%I64d\n",sum);
}
return 0;
}

codeforces629C Famil Door and Brackets (dp)的更多相关文章

  1. 【Codeforces629C】Famil Door and Brackets [DP]

    Famil Door and Brackets Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample Inp ...

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

    C. Famil Door and Brackets 题目连接: http://www.codeforces.com/contest/629/problem/C Description As Fami ...

  3. codeforces 629C Famil Door and Brackets (dp + 枚举)

    题目链接: codeforces 629C Famil Door and Brackets 题目描述: 给出完整的括号序列长度n,现在给出一个序列s长度为m.枚举串p,q,使得p+s+q是合法的括号串 ...

  4. codeforces629C Famil Door and Brackets (dp)

    题意:给你一个长度为n的括号匹配串(不一定恰好匹配),让你在这个串的前面加p串和后面加上q串,使得这个括号串平衡(平衡的含义是对于任意位置的括号前缀和大于等于0,且最后的前缀和为0). 思路:枚举这个 ...

  5. Codeforces 629C Famil Door and Brackets DP

    题意:给你一个由括号组成的字符串,长度为m,现在希望获得一个长度为n(全由括号组成)的字符串,0<=n-m<=2000 这个长度为n的字符串要求有两个性质:1:就是任意前缀,左括号数量大于 ...

  6. Codeforces629 C. Famil Door and Brackets

    C. Famil Door and Brackets time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  7. Codeforces 629C Famil Door and Brackets(DP)

    题目大概说给一个长m的括号序列s,要在其前面和后面添加括号使其变为合法的长度n的括号序列,p+s+q,问有几种方式.(合法的括号序列当且仅当左括号总数等于右括号总数且任何一个前缀左括号数大于等于右括号 ...

  8. 【23.24%】【codeforces 629C】Famil Door and Brackets

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. ZOJ 2604 Little Brackets DP

    DP: 边界条件:dp[0][j] = 1 递推公式:dp[i][j] = sum{dp[i-k][j] * dp[k-1][j-1] | 0<k≤i} i对括号深度不超过j的,能够唯一表示为( ...

随机推荐

  1. 【剑指 Offer】06.从尾到头打印链表

    题目描述 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回). 示例 1: 输入:head = [1,3,2] 输出:[2,3,1] 限制: 0 <= 链表长度 <= 10 ...

  2. golang遍历时修改被遍历对象

    目录 前言 遍历切片 遍历map 总结 前言 很多时候需要将遍历对象中去掉某些元素,或者往遍历对象中添加元素,这时候就需要小心操作了. 对于go语言中的一些注意事项我做了总结和示例,留下点笔记. 遍历 ...

  3. self-taught CS resouce recommendation

    https://github.com/keithnull/TeachYourselfCS-CN/blob/master/TeachYourselfCS-CN.md#%E8%AE%A1%E7%AE%97 ...

  4. LeetCode530. 二叉搜索树的最小绝对差

    题目 又是常见的BST,要利用BST的性质,即中序遍历是有序递增序列. 法一.中序遍历 1 class Solution { 2 public: 3 vector<int>res; 4 v ...

  5. Vulnhub靶场——DC-1

    记一次Vulnhub靶场练习记录 靶机DC-1下载地址: 官方地址 https://download.vulnhub.com/dc/DC-1.zip 该靶场共有5个flag,下面我们一个一个寻找 打开 ...

  6. SAP里会话结束方法(杀死进程)

    在SAP的ERP里,有很多方法可以结束一个会话,然而在不同情况下,需要使用的方法也不同.下面从先后顺序来简单说明:1.SM04:最常用的方法,在SM04点击工具栏的会话->结束会话,来关闭一个会 ...

  7. 开发中的你的Git提交规范吗?

    1. 前言 目前大部分公司都在使用Git作为版本控制,每个程序员每天都要进行代码的提交.很多开发者也包括我自己,有时候赶时间或者图省事,就这么提交: git commit -m "修改bug ...

  8. Effective Java, 3e阅读笔记一

    引言 本书的目标是帮助读者更加有效地使用Java编程语言及其基本类库,适用于任何具有实际Java工作经验的程序员. 本书一共90个条目,12章,每个条目讨论一条规则,这些规则反映了最有经验的优秀程序员 ...

  9. 大数据系列2:Hdfs的读写操作

    在前文大数据系列1:一文初识Hdfs中,我们对Hdfs有了简单的认识. 在本文中,我们将会简单的介绍一下Hdfs文件的读写流程,为后续追踪读写流程的源码做准备. Hdfs 架构 首先来个Hdfs的架构 ...

  10. 使用cacti监控linux主机

    介绍:使用cacti监控linux主机,需要在linux主机上面安装snmp服务,并修改snmpd.conf文件,指定cacti服务器的地址,然后在cacti的前台界面添加此主机即可,此处以监控cen ...