Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define sz(s) (int)(s).size()
- #define all(s) s.begin(),s.end()
- void Speed() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- }
- const int mod = 1e9 + 7;
- vector<pair<int,int>> arr(40);
- int n;
- const int N = 30 + 1;
- ll dp[N][N][N][N * N];
- ll solve(int i , int op , int cl , int sum){
- if(i == n){
- return sum == 0;
- }
- if(sum < 0)
- return 0;
- ll &ret = dp[i][op][cl][sum];
- if(~ret)
- return ret;
- ll ans = 0;
- ans = (ans + solve(i + 1, arr[i + 1].first , arr[i + 1].second , sum)) % mod;
- if(op > 0)
- ans = (ans + op * solve(i , op - 1 , cl , sum + 1) % mod) % mod;
- if(cl > 0)
- ans = (ans + cl * solve(i , op , cl - 1, sum - 1) % mod) % mod;
- return ret = ans;
- }
- void solve() {
- memset(dp,-1,sizeof dp);
- cin >> n;
- for(int i = 0; i < n; i++){
- string s;
- cin >> s;
- int op = 0 , cl = 0;
- for(auto x : s)
- if(x == '(')
- op++;
- else cl++;
- arr[i] = {op,cl};
- }
- cout << solve(0 , arr[0].first , arr[0].second , 0) << "\n";
- }
- int main() {
- freopen("count.in", "r", stdin);
- Speed();
- int tc = 1;
- //cin >> tc;
- while (tc--) {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment