-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoons_and_umbrellas.cpp
59 lines (47 loc) · 985 Bytes
/
moons_and_umbrellas.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
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
int X, Y;
int memo[2][1001];
bool ok[2][1001];
string str;
int dp(const char prev, const int i) {
if (i == (int)str.size()) return 0;
if (ok[prev == 'J'][i]) return memo[prev == 'J'][i];
int res;
const int j = X * (prev == 'C') + dp('J', i + 1);
const int c = Y * (prev == 'J') + dp('C', i + 1);
switch (str[i]) {
case 'J':
res = j;
break;
case 'C':
res = c;
break;
default:
res = min(c, j);
}
ok[prev == 'J'][i] = true;
return memo[prev == 'J'][i] = res;
}
void solve() {
memset(ok, 0, sizeof ok);
cin >> X >> Y;
cin >> str;
int ans = INT_MAX;
if (str.front() == '?') {
ans = min(dp('C', 1), dp('J', 1));
} else {
ans = dp(str.front(), 1);
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T;
cin >> T;
for (int t = 0; t < T; t++) {
cout << "Case #" << t + 1 << ": ";
solve();
}
}