USACO 2014 Gold November P1. Balanced Cow Breeds

Advanced DP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxV = 1001;
int dp[maxV][maxV];

int main() {
    string s;
    cin >> s;

    dp[1][1] = 1, dp[1][0] = 1;

    int currOpen = 1;
    int i = 0;
    for (auto c : s) {
        i++;
        if (i < 2) {
            continue;
        }
        if (c == '(') {
            currOpen++;
        }
        else {
            currOpen--;
        }
        for (int j = 0; j < currOpen + 1; j++) {
            int k = currOpen - j;
            int l = currOpen - k + 1;
            dp[i][j] = 0;

            if (c == '(') {
                // left bracket, "("

                if (j - 1 >= 0 && dp[i - 1][j - 1] != 0) {
                    dp[i][j] += dp[i - 1][j - 1];
                }
                if (k - 1 >= 0 && dp[i - 1][currOpen - 1 - (k - 1)] != 0) {
                    dp[i][j] += dp[i - 1][currOpen - 1 - (k - 1)];
                }
            }
            else {
                // right bracket, ")"

                if (dp[i - 1][j + 1] != 0) {
                    dp[i][j] += dp[i - 1][j + 1];
                }
                if (dp[i - 1][currOpen + 1 - (k + 1)] != 0) {
                    dp[i][j] += dp[i - 1][currOpen + 1 - (k + 1)];
                }
            }
            dp[i][j] %= 2012;
        }
    }

    cout << dp[i][0] % 2012;
}