CodeForces - 629C Famil Door and Brackets(dp)

22 篇文章 1 订阅

Famil Door and Brackets

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
Copy
4 1
(
output
4
input
Copy
4 4
(())
output
1
input
Copy
4 3
(((
output
0
Note

In the first sample there are four different valid pairs:

  1. p = "(", q = "))"
  2. p = "()", q = ")"
  3. p = "", q = "())"
  4. 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.


题意:在输入的s串前后加上串p和串q(可为空),使之任何一个前缀的"("都不少于")",切总的"("和")"相等.

思路:dp,dp[i][j]表示前i个字符组成的串值为j的有dp[i][j],其中"("代表1,")"代表-1,预处理dp数组,然后枚举前缀长度和串值,从而也可以确定后缀长度和串值. 当然前缀的串值大小一定大于等于0且与s串值加和大于等于0.

转移方程:dp[i][j] = dp[i-1][j+1]+dp[i-1][j-1]

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

int n,m;
char s[maxn];
ll dp[3001][3002];

int main()
{
	cin>>n>>m;
	scanf(" %s",s);
	
	int ws = 0,minw = ff;
	for(int i = 0;i< m;i++)
	{
		if(s[i] == '(')
			ws++;
		else
			ws--;
		minw = min(minw,ws);//保留最小值 
	}
	
	dp[0][0] = 1;
	for(int i = 1;i<= 2003;i++)
	{
		for(int j = 0;j<= i;j++)
		{
			dp[i][j]+= dp[i-1][j+1];
			if(j>= 1)
				dp[i][j]+= dp[i-1][j-1];
			dp[i][j]%= mod;
		}
	}
	
	ll ans = 0;
	for(int i = 0;i<= n-m;i++)
	{
		for(int j = max(0,-minw);j<= i;j++)
		{
			if(j+ws<= n-m-i)
				ans = (ans+dp[i][j]*dp[n-(i+m)][(j+ws)])%mod;//按理说应该是dp[n-(i+m)][-(j+ws)],但是它和正的相等 
		}
	}
	
	cout<<ans<<endl;
	
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值