Advertisement
Dang_Quan_10_Tin

TGBRACKETS

Jun 17th, 2022
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #define task "TGBRACKETS"
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. constexpr int N = 5e2 + 5;
  12. constexpr int mod = 1e9 + 7;
  13. int n;
  14. string s;
  15. int dp[N][N][N / 2];
  16.  
  17. void Read()
  18. {
  19.     cin >> n >> s;
  20.     s = " " + s;
  21. }
  22.  
  23. #define Match(x, y) (x != y)
  24.  
  25. int f(int x, int y, int sum)
  26. {
  27.     int &ans = dp[x][y][sum];
  28.     if (ans != -1)
  29.         return ans;
  30.  
  31.     ans = 0;
  32.  
  33.     if (x >= y)
  34.         return ans = 1;
  35.  
  36.     ans = ((f(x + 1, y, sum) + f(x, y - 1, sum)) % mod - f(x + 1, y - 1, sum)) % mod;
  37.  
  38.     if (Match(s[x], s[y]) && (s[x] == '(' || sum > 0))
  39.         (ans += f(x + 1, y - 1, sum + (s[x] == '(' ? 1 : -1))) %= mod;
  40.  
  41.     return ans;
  42. }
  43.  
  44. void Solve()
  45. {
  46.     memset(dp, -1, sizeof dp);
  47.  
  48.     cout << (f(1, n, 0) - 1 + mod) % mod;
  49. }
  50.  
  51. int32_t main()
  52. {
  53.     ios_base::sync_with_stdio(0);
  54.     cin.tie(0);
  55.     cout.tie(0);
  56.  
  57.     if (fopen(task ".INP", "r"))
  58.     {
  59.         freopen(task ".INP", "r", stdin);
  60.         freopen(task ".OUT", "w", stdout);
  61.     }
  62.  
  63.     Read();
  64.     Solve();
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement