思路:没用完全背包优化的动态规划只能O(n^3)只能过 40%
#include<bits/stdc++.h>
using namespace std;
const int N = 5e3+7;
const int mod = 1e9+7;
string s;
int len;
int num[N],dp[N][N];
int solve(int need,int flag){
memset(num, 0, sizeof(num));
memset(dp, 0, sizeof(dp));
if(!flag){
reverse(s.begin(),s.end());
for(int i=0;i<len;i++){
if(s[i]=='(')s[i]=')';
else s[i]='(';
}
}
int left = 0,cnt = 0;
for(int i=0;i<len;i++){
if(s[i]=='(')left++;
if(s[i]==')') num[++cnt] = left;
}
if(num[1]>0)dp[1][0]=1;
for(int i=1;i<=cnt;i++){
dp[1][i] = 1;
}
for(int i=2;i<=cnt;i++){
for(int j = ((0ll>i-num[i])?0ll:i-num[i]) ; j<=need ; j++){
for(int k = 0;k<=j;k++){
dp[i][j] += dp[i-1][k];
dp[i][j] %= mod;
}
}
}
return dp[cnt][need];
}
int main( ){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>s;
len = s.size();
int need1=0,need2=0;
int left = 0,right = 0;
for(int i=0;i<len;i++){
if(s[i]=='(')left++;
else left--;
if(left<0)need1++,left=0;
}
for(int j=len-1;j>=0;j--){
if(s[j]==')')right++;
else right--;
if(right<0)need2++,right=0;
}
cout<<solve(need1,1)*solve(need2,0)%mod<<endl;
return 0;
}