比赛 20241025 评测结果 AAAAAAAAAAAAAAAA
题目名称 Pair Programming 最终得分 100
用户昵称 darkMoon 运行时间 2.037 s
代码语言 C++ 内存使用 98.60 MiB
提交时间 2024-10-25 11:27:49
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
using namespace std;
auto IN = freopen("prob2_gold_22open.in", "r", stdin);
auto OUT = freopen("prob2_gold_22open.out", "w", stdout);
auto mread = [](){int x;scanf("%lld", &x);return x;};
const int N = 2005, MOD = 1e9 + 7;
int t = mread(), f[N][N][3], s[N][N][3];
signed main(){
    while(t --){
        int n = mread();
        vector<int> a(1, 0), b(1, 0);
        string s1, s2;
        cin >> s1 >> s2;
        for(int i = 0; i < n; i ++){
            if(s1[i] != '1'){
                if(s1[i] == '+'){
                    a.push_back(-1);
                }
                else{
                    a.push_back(s1[i] - '0');
                }
            }
        }
        for(int i = 0; i < n; i ++){
            if(s2[i] != '1'){
                if(s2[i] == '+'){
                    b.push_back(-1);
                }
                else{
                    b.push_back(s2[i] - '0');
                }
            }
        }
        int laa[n + 5][3] = {0}, lab[n + 5][3] = {0};
        for(int i = 1; i < a.size(); i ++){
            laa[i][0] = laa[i - 1][0];
            laa[i][1] = laa[i - 1][1];
            laa[i][2] = laa[i - 1][2];
            if(a[i] == 0){
                laa[i][0] = i;
            }
            else if(a[i] == -1){
                laa[i][2] = i;
            }
            else{
                laa[i][1] = i;
            }
        }
                // 0: 0      1: 数字      2: 字母
        for(int i = 1; i < b.size(); i ++){
            lab[i][0] = lab[i - 1][0];
            lab[i][1] = lab[i - 1][1];
            lab[i][2] = lab[i - 1][2];
            if(b[i] == 0){
                lab[i][0] = i;
            }
            else if(b[i] == -1){
                lab[i][2] = i;
            }
            else{
                lab[i][1] = i;
            }
        }
        for(int i = 0; i <= n; i ++){
            for(int j = 0; j <= n; j ++){
                f[i][j][0] = f[i][j][1] = f[i][j][2] = 0;
                s[i][j][0] = s[i][j][1] = s[i][j][2] = 0;
            }
        }
        f[0][0][0] = 1;
        s[0][0][0] = 1;
        // 0: 是 0      1: 最后是 数字      2: 最后是 字母
        for(int i = 0; i < a.size(); i ++){
            for(int j = 0; j < b.size(); j ++){
                if(i == 0 && j == 0){
                    continue;
                }
                if(i == 0){
                    s[i][j][0] = s[i][j - 1][0];
                    s[i][j][1] = s[i][j - 1][1];
                    s[i][j][2] = s[i][j - 1][2];
                }
                else if(j == 0){
                    s[i][j][0] = s[i - 1][j][0];
                    s[i][j][1] = s[i - 1][j][1];
                    s[i][j][2] = s[i - 1][j][2];
                }
                else{
                    s[i][j][0] = (s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0] + MOD) % MOD;
                    s[i][j][1] = (s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1] + MOD) % MOD;
                    s[i][j][2] = (s[i - 1][j][2] + s[i][j - 1][2] - s[i - 1][j - 1][2] + MOD) % MOD;
                }
                // if(一个 0 都不能有){
                //     if(全都是数字){
                //         (f[i][j][1] += f[i1 - 1][j1 - 1][2]) %= MOD;
                //     }
                //     else if(全都是字母){
                //         (f[i][j][2] += f[i1 - 1][j1 - 1][1] + f[i1 - 1][j1 - 1][0]) %= MOD;
                //     }
                // }
                int t1 = max(laa[i][0], laa[i][2]), t2 = max(lab[j][0], lab[j][2]);
                f[i][j][1] = s[i][j][2];
                if(t2 > 0){
                    f[i][j][1] = (f[i][j][1] - s[i][t2 - 1][2] + MOD) % MOD;
                }
                if(t1 > 0){
                    f[i][j][1] = (f[i][j][1] - s[t1 - 1][j][2] + MOD) % MOD;
                }
                if(t1 > 0 && t2 > 0){
                    f[i][j][1] = (f[i][j][1] + s[t1 - 1][t2 - 1][2]) % MOD;
                }

                t1 = max(laa[i][0], laa[i][1]), t2 = max(lab[j][0], lab[j][1]);
                f[i][j][2] = (s[i][j][1] + s[i][j][0]) % MOD;
                if(t2 > 0){
                    f[i][j][2] = (f[i][j][2] - s[i][t2 - 1][1] - s[i][t2 - 1][0] + MOD + MOD) % MOD;
                }
                if(t1 > 0){
                    f[i][j][2] = (f[i][j][2] - s[t1 - 1][j][1] - s[t1 - 1][j][0] + MOD + MOD) % MOD;
                }
                if(t1 > 0 && t2 > 0){
                    f[i][j][2] = (f[i][j][2] + s[t1 - 1][t2 - 1][1] + s[t1 - 1][t2 - 1][0]) % MOD;
                }
                // 0: 0      1: 数字      2: 字母
                // 如果有一串 数字 前面紧接着 0,那么就可以是 0
                if(laa[i][0] != 0 && laa[i][0] >= laa[i][2]){
                    f[i][j][0] = 1;
                }
                if(lab[j][0] != 0 && lab[j][0] >= lab[j][2]){
                    f[i][j][0] = 1;
                }
                if(laa[i][2] == 0 && lab[j][2] == 0){
                    f[i][j][0] = 1;
                }
                (s[i][j][0] += f[i][j][0]) %= MOD;
                (s[i][j][1] += f[i][j][1]) %= MOD;
                (s[i][j][2] += f[i][j][2]) %= MOD;
                // printf("*** %lld %lld %lld %lld %lld %lld %lld\n", i, j, f[i][j][0], f[i][j][1], f[i][j][2], t1, t2);
            }
        }
        printf("%lld\n", (f[a.size() - 1][b.size() - 1][0] + f[a.size() - 1][b.size() - 1][1] + f[a.size() - 1][b.size() - 1][2]) % MOD);
    }
    return 0;
}