思路:
似乎和某次培训的题很像啊。。。
将左括号记为1,右括号记为-1,那么最终一定加和为0,然后再求最小前缀和。
用dp解决即可。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1000010;
const int mod = 1e9+7;
char s[maxn];
int n,m;
ll sum;
int mn;
ll dp[2010][2010];
ll ans;
int main () {
#ifdef ONLINE_JUDGE
freopen("bracket.in","r",stdin);
freopen("bracket.out","w",stdout);
#endif
scanf("%d %d",&n,&m);
scanf("%s",s+1);
for(int i = 1;i <= m; ++i) {
if(i == 1) mn = (s[i] == '(' ? 1 : -1);
sum += (s[i] == '(' ? 1 : -1);
mn = min(mn,(int)sum);
}
dp[0][0] = 1;
for(int i = 1;i <= n - m; ++i) {
for(int j = 0;j <= i; ++j) {
if(!j) {
dp[i][j] = dp[i - 1][j + 1];
}
else dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j + 1]) % mod;
}
}
for(int i = 0;i <= n - m; ++i) {
for(int j = 0;j <= i; ++j) {
if(j + sum <= n - m && j + mn >= 0) {
ans = (ans + dp[i][j] * dp[n - m - i][j + sum])%mod;
}
}
}
cout<<ans<<endl;
return 0;
}