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
|
#include <bits/stdc++.h> using namespace std; #define resetIO(x) \ freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout) #define debug(fmt, ...) \ fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) template <class _Tp> inline _Tp& read(_Tp& x) { bool sign = false; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) sign |= (ch == '-'); for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48); return sign ? (x = -x) : x; } template <class _Tp> inline void write(_Tp x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar((x % 10) ^ 48); } bool m_be; using ll = long long; const int MAXN = 2e3 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int n; char s1[MAXN], s2[MAXN]; inline int add(int x, int y) { return (x += y), (x >= MOD ? x - MOD : x); } inline int sub(int x, int y) { return (x -= y), (x < 0 ? x + MOD : x); } struct Node { int cnt; bool zero; Node operator+(const Node& o) const { return {add(cnt, o.cnt), zero || o.zero}; } Node& operator+=(const Node& o) { return cnt = add(cnt, o.cnt), zero |= o.zero, *this; } Node operator-(const Node& o) const { return {sub(cnt, o.cnt), zero}; } Node& operator-=(const Node& o) { return cnt = sub(cnt, o.cnt), *this; } Node extend(char c) const { if (c == '0') return {0, 1}; if (isdigit(c)) return {cnt, zero}; return {add(cnt, zero), 0}; } } dp[MAXN][MAXN];
inline bool same(char c1, char c2) { if (isdigit(c1) == isdigit(c2)) return 1; if (c1 == '1' || c2 == '1') return 1; return 0; } bool m_ed; signed main() { int cas; read(cas); while (cas--) { scanf("%d%s%s", &n, s1 + 1, s2 + 1); for (int i = 0; i <= n; ++i) for (int j = 0; j <= n; ++j) dp[i][j] = {0, 0}; dp[0][0] = {0, 1}; for (int i = 0; i <= n; ++i) { for (int j = 0; j <= n; ++j) { if (i) dp[i][j] += dp[i - 1][j].extend(s1[i]); if (j) dp[i][j] += dp[i][j - 1].extend(s2[j]); if (i && j && same(s1[i], s2[j])) dp[i][j] -= dp[i - 1][j - 1].extend(s1[i]).extend(s2[j]); } } write(add(dp[n][n].cnt, dp[n][n].zero)), putchar('\n'); } return 0; }
|