-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreeParentheses_UVA1238_AC.cpp
53 lines (48 loc) · 1.15 KB
/
freeParentheses_UVA1238_AC.cpp
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
#include <bits/stdc++.h>
using namespace std;
int num[35], sign[35], cnt;
int memo[35][35][6500];
set<int> S;
void dp(int open, int n, int value)
{
if (memo[open][n][value + 3200] != -1)
return;
if (n == cnt - 1)
{
S.insert(value);
return;
}
int nval = sign[n + 1] * num[n + 1] * (open % 2 == 0 ? 1 : -1);
if (open > 0)
dp(open - 1, n + 1, value + nval);
if (sign[n + 1] == -1)
dp(open + 1, n + 1, value + nval);
dp(open, n + 1, value + nval);
memo[open][n][value + 3200] = 0;
}
int main()
{
freopen("freeParentheses_UVA1238.in", "r", stdin);
freopen("freeParentheses_UVA1238.out", "w", stdout);
string s;
char c;
while (getline(cin, s))
{
stringstream sin;
sin << s;
sin >> num[0];
S.clear();
sign[0] = 1;
cnt = 1;
while (sin >> c)
{
sign[cnt] = c == '-' ? -1 : 1;
sin >> num[cnt];
cnt++;
}
memset(memo, -1, sizeof memo);
dp(0, 0, num[0]);
printf("%d\n", (int)S.size());
}
return 0;
}