Advertisement
kolbka_

Untitled

Dec 10th, 2021
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. int main() {
  2. ios_base::sync_with_stdio(0);
  3. cin.tie(0);
  4. cout.tie(0);
  5. string x;
  6. cin >> x;
  7. int64_t n = x.size();
  8. vector<vector<int64_t>> dp(2, vector<int64_t>(n / 2 +2));
  9. dp[0][0] = 1;
  10. for (int i = 1; i <= n; i++) {
  11. for (int balance = 0; balance <= n / 2; balance++) {
  12. if (x[i - 1] == '(') {
  13. if (balance >= 1)
  14. dp[i % 2][balance] = dp[(i - 1) % 2][balance - 1];
  15. } else if (x[i - 1] == ')') {
  16. dp[i % 2][balance] = dp[(i - 1) % 2][balance + 1];
  17. } else {
  18. if (balance >= 1)
  19. dp[i % 2][balance] =
  20. dp[(i - 1) % 2][balance - 1] + dp[(i - 1) % 2][balance + 1];
  21. else
  22. dp[i % 2][balance] = dp[(i - 1) % 2][balance + 1];
  23.  
  24. }
  25. dp[i % 2][balance] = dp[i % 2][balance] % 1000000007;
  26. }
  27. fill(dp[(i-1)%2].begin(), dp[(i-1)%2].end(), 0);
  28. }
  29. cout << (dp[n % 2][0]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement