-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.cpp
99 lines (77 loc) · 2.2 KB
/
03.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
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;
const ll mod = 1e9 + 7;
void solve() {
ll n;
cin >> n;
string s;
string t;
cin >> s;
cin >> t;
reverse(s.begin(), s.end());
reverse(t.begin(), t.end());
s += '0';
t += '0';
n++;
vi psum(n + 1);
for(int i = 0; i < n; i++) {
psum[i + 1] = psum[i] + (s[i] == '+');
}
vi psum2(n + 1);
for(int i = 0; i < n; i++) {
psum2[i + 1] = psum2[i] + (t[i] == '+');
}
vi psum3(n + 1);
for (int i = 0; i < n; i++) {
psum3[i + 1] = psum3[i] + (s[i] == '0');
}
vi psum4(n + 1);
for (int i = 0; i < n; i++) {
psum4[i + 1] = psum4[i] + (t[i] == '0');
}
ll tot = 0;
vector<vi> dp(n + 1, vi(n + 1));
vector<vb> used(n + 1, vb(n + 1));
// might be wrong
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// move up on i
if (s[i] == '+' && psum3[i] == 0 && psum4[j] == 0) {
dp[psum[i + 1]][psum2[j]] += dp[psum[i]][psum2[j]];
dp[psum[i + 1]][psum2[j]] %= mod;
// tot += dp[i][j];
// tot %= mod;
} else if (s[i] == '0' && !used[psum[i]][psum2[j]] && psum3[i] == 0 && psum4[j] == 0) {
tot += dp[psum[i]][psum2[j]];
used[psum[i]][psum2[j]] = true;
}
// move up on j
if (t[j] == '+' && psum3[i] == 0 && psum4[j] == 0) {
dp[psum[i]][psum2[j + 1]] += dp[psum[i]][psum2[j]];
dp[psum[i]][psum2[j + 1]] %= mod;
// tot += dp[i][j];
// tot %= mod;
} else if (t[j] == '0' && !used[psum[i]][psum2[j]] && psum3[i] == 0 && psum4[j] == 0) {
tot += dp[psum[i]][psum2[j]];
used[psum[i]][psum2[j]] = true;
}
}
}
tot %= mod;
if (tot < 0) tot += mod;
cout << tot << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
while(t--) solve();
return 0;
}