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 strings 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.
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.
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.
4 1 (
4
4 4 (())
1
4 3 (((
0
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.
定义一个合法串:
(1),'('的总数 == ')'的总数。
(2), 串中所有前缀 满足 '('的个数 >= ')'的个数。
题意:给定一个合法串的长度n和一个长度为m的中间串s,要求你找到一个p串和q串,使得p + s + q是一个合法串且长度为n。问有多少种不同的方案数。 结果 % (1e9+7)。
思路:设置dp[i][j]表示长度为i的合法串且'('的个数 - ')'的个数 == j的方案数。
我们称j为该串的平衡度。其实dp[i][j] = dp[i][-j],因为'('和')'可以互换。
转移有dp[i][j] = dp[i-1][j+1] + dp[i-1][j-1]。
那么我们可以枚举p串的长度和平衡度,找到合法的q串。再统计方案数。
注意:我们不能仅仅要求p + s + q合法就够了,必须保证选出的p + s也是一个合法串。
假设p + s长度为i且平衡度为j(j>=0)。那么q串长度为n-m-i,平衡度为-j。
AC代码;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <string>
#include <stack>
using namespace std;
typedef long long LL;
const int MAXN = 2005;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
void add(LL &x, LL y) {x += y, x %= MOD;}
LL dp[MAXN][MAXN];
void getdp()
{
memset(dp, 0LL, sizeof(dp)); dp[0][0] = 1LL;
for(int i = 1; i <= 2000; i++)
{
dp[i][0] = dp[i-1][1];
for(int j = 1; j <= i; j++)
add(dp[i][j], (dp[i-1][j+1]+dp[i-1][j-1]) % MOD);
}
}
int main()
{
getdp();
int n, m; cin >> n >> m;
string str; cin >> str;
stack<int> S; int sum = 0;
int Min = INF;
for(int i = 0; i < m; i++)
{
if(str[i] == '(') sum++;
else sum--;
Min = min(Min, sum);
}
int len = n - m; LL ans = 0LL;
for(int i = 0; i <= len; i++)
{
for(int j = 0; j <= i; j++)
{
if(j + Min < 0 || len - i < j + sum) continue;
add(ans, dp[i][j] * dp[len-i][j+sum] % MOD);
}
}
cout << ans << endl;
return 0;
}