time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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.

Examples

input

4 1

(

output

4

input

4 4

(())

output

1

input

4 3

(((

output

0

Note

In the first sample there are four different valid pairs:

p = “(“, q = “))”

p = “()”, q = “)”

p = “”, q = “())”

p = “”, q = “)()”

In the second sample the only way to obtain a desired string is choose empty p and q.

In the third sample there is no way to get a valid sequence of brackets.

【题解】



给你一个串;

只包含圆括号;

可以让你在最左边和最右边加上两个串p和q;

这两个串也只能包括括号;

要使得最后的括号是匹配的;

问你有多少对P和q,p和q可以为空

每时每刻左括号的数目都要大于等于右括号的数目;

最后那个限制起了很大的作用;

把左括号看做1,右括号看做-1;

则它的要求是每时每刻前缀和都大于0;

则先求出所给的s的前缀和,并记录前缀和的最小值mi;

预处理出dp[i][j]表示i个括号能够造出前缀和为j的方案数(满足每时每刻前缀和都大于等于0的方案);

然后枚举q的长度i,前缀和为j

如果dp[i][j]+mi>=0;

则从左到中间那个串都是满足题意的了(前缀和始终大于等于0)

然后再获取P;

p的长度就是n-m-i;

它的前缀和该是啥?j+pres;

这里的j+pres指的是负数->加上dp[n-m-i][j+pres];

可以理解为从右到左进行DP;然后从右到左的过程中每时每刻右括号的数目都大于左括号的数目;(等价转化);

这样可以保证从最左边的q到最右边的p的过程中间都不会出现前缀和小于0的情况;

假设在p处出现了前缀和小于0;则在这个位置的右边右括号的数目大于左括号的数目;最后结果就不可能为0;只可能是负数;

这和j+pres-(j+pres)==0不符合;

所以在这个串中不会出现前缀和为负数的情况;

具体实现看代码;

(那个DP很简单的。。);

#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define LL long long using namespace std; const int MAXN = 2000;
const LL MOD = 1e9+7; int n,m;
LL dp[MAXN+20][MAXN+20];
string s; const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
void input_LL(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input_int(n);input_int(m);
cin >> s;
int pres = 0,mi=0;
for (int i = 0;i<=m-1;i++)
{
if (s[i]=='(')
pres++;
else
pres--;
mi = min(mi,pres);
}
dp[0][0] = 1;
for (int i = 1;i <= 2000;i++)
for (int j = 0;j <= i;j++)
{
if (j)
dp[i][j]=(dp[i][j]+dp[i-1][j-1])%MOD;//add "("
dp[i][j] = (dp[i][j]+dp[i-1][j+1]) % MOD;//add ")"
} LL ans = 0;
for (int p = 0;p <= n-m;p++)
for (int j = 0;j <= p;j++)
if (mi+j>=0 && j+pres<=(n-m))
ans = (ans + dp[p][j]*dp[n-m-p][pres+j])%MOD;
cout << ans << endl;
return 0;
}

【23.24%】【codeforces 629C】Famil Door and Brackets的更多相关文章

  1. JAVA 基础编程练习题24 【程序 24 根据输入求输出】

    24 [程序 24 根据输入求输出] 题目:给一个不多于 5 位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. package cskaoyan; public class cskaoya ...

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. 【23. 合并K个排序链表】【困难】【优先队列/堆排序】

    合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6] 输出: 1->1-> ...

  4. 【20.23%】【codeforces 740A】Alyona and copybooks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【23.33%】【codeforces 557B】Pasha and Tea

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【23.39%】【codeforces 558C】Amr and Chemistry

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【24.17%】【codeforces 721D】Maxim and Array

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

  8. 【24.67%】【codeforces 551C】 GukiZ hates Boxes

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

  9. 【24.34%】【codeforces 560D】Equivalent Strings

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

随机推荐

  1. Loading half a billion rows into MySQL---转载

    Background We have a legacy system in our production environment that keeps track of when a user tak ...

  2. JS错误记录 - 微博发布

    <style> *{ margin: 0; padding: 0;} #ul1{ width: 400px; height: 400px; border: 1px solid #000; ...

  3. Maven使用yuicompressor-maven-plugin打包压缩css、js文件

    最近项目想使用在maven打包的时间压缩js,css文件,采用yuicompressor-maven-plugin插件进行压缩,但只是压缩减小大小,提高请求速度,并没有对js进行混淆.下面就写一下这个 ...

  4. js进阶 12-17 jquery实现鼠标左键按下拖拽功能

    js进阶 12-17 jquery实现鼠标左键按下拖拽功能 一.总结 一句话总结:监听的对象必须是文档,鼠标按下运行mousemove事件,鼠标松开取消mousemove事件的绑定,div的偏移的话是 ...

  5. js中退出语句break,continue和return 比较 (转)

    在 break,continue和return 三个关键字中, break,continue是一起的,return 是函数返回语句,但是返回的同时也将函数停止 首先:break和continue两个一 ...

  6. 23种设计模式——Prototype模式

    Prototype模式是提供自我复制的功能.包括浅拷贝和深拷贝. 一.Prototype模式的用途 场景1:游戏场景中有很多类似的敌人,它们的技能都一样,但是随着敌人出现的位置和不同,它们的能力也不太 ...

  7. ios开发网络学习十二:NSURLSession实现文件上传

    #import "ViewController.h" // ----WebKitFormBoundaryvMI3CAV0sGUtL8tr #define Kboundary @&q ...

  8. TextView之二:常用属性 分类: H1_ANDROID 2013-10-30 12:43 3203人阅读 评论(0) 收藏

    参考自<疯狂android讲义>2.3节 //TextView所呈现的文字 android:text="我爱Java" //文字颜色 android:textColor ...

  9. springMVC注解@initbinder

    在实际操作中经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能. 比 ...

  10. php面试题11(边看边复习刚刚讲的)(array_multisort($arr1,$arr2); 用$arr1来排序$arr2。)

    php面试题11(边看边复习刚刚讲的)(array_multisort($arr1,$arr2); 用$arr1来排序$arr2.) 一.总结 1.边看边复习刚刚讲的 2.array_multisor ...