Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- string x;
- cin >> x;
- int64_t n = x.size();
- vector<vector<int64_t>> dp(2, vector<int64_t>(n / 2 +2));
- dp[0][0] = 1;
- for (int i = 1; i <= n; i++) {
- for (int balance = 0; balance <= n / 2; balance++) {
- if (x[i - 1] == '(') {
- if (balance >= 1)
- dp[i % 2][balance] = dp[(i - 1) % 2][balance - 1];
- } else if (x[i - 1] == ')') {
- dp[i % 2][balance] = dp[(i - 1) % 2][balance + 1];
- } else {
- if (balance >= 1)
- dp[i % 2][balance] =
- dp[(i - 1) % 2][balance - 1] + dp[(i - 1) % 2][balance + 1];
- else
- dp[i % 2][balance] = dp[(i - 1) % 2][balance + 1];
- }
- dp[i % 2][balance] = dp[i % 2][balance] % 1000000007;
- }
- fill(dp[(i-1)%2].begin(), dp[(i-1)%2].end(), 0);
- }
- cout << (dp[n % 2][0]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement