-
Notifications
You must be signed in to change notification settings - Fork 0
/
C.cpp
86 lines (80 loc) · 1.64 KB
/
C.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200007;
const int MOD = 1000000007;
#define wat(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
int flip(int n) {
if (n == 0) {return 1;}
return 0;
}
void solve() {
string s;
cin >> s;
int n = s.size();
long long dp[n + 7];
dp[0] = 1;
long long curr = -1, qcount = 1, qs = 0;
if (s[0] == '0') {curr = 1;}
if (s[0] == '1') {curr = 0;}
if (s[0] == '?') {qs++;}
for (int i = 1; i < n; i++) {
if (s[i - 1] == '?') {
if (curr == -1) {
if (s[i] == '0') {curr = 1;}
if (s[i] == '1') {curr = 0;}
dp[i] = dp[i - 1] + qcount + 1;
qcount++;
}
else {
if (s[i] == '0' + curr || s[i] == '?') {
curr = flip(curr);
dp[i] = dp[i - 1] + qcount + 1;
qcount++;
}
else {
// we lost
dp[i] = dp[i - 1] + qs + 1;
qcount = qs + 1; // check?
}
}
}
else if (s[i - 1] == '0') {
if (s[i] == '0') {
// we can only do one
dp[i] = dp[i - 1] + 1;
curr = 1;
qcount = 1;
}
else {
// we'll continue
dp[i] = dp[i - 1] + qcount + 1;
curr = 0;
qcount++;
}
}
else {
if (s[i] == '1') {
// we can only do one
dp[i] = dp[i - 1] + 1;
curr = 0;
qcount = 1;
}
else {
// we'll continue
dp[i] = dp[i - 1] + qcount + 1;
curr = 1;
qcount++;
}
}
if (s[i] == '?') {qs++;}
else {qs = 0;}
// cout << wat(i) << wat(s[i]) << wat(dp[i]) << wat(curr) << wat(qcount) << wat(qs) << endl;
}
cout << dp[n - 1] << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
//solve();
}