题解:P12472 [集训队互测 2024] 基础 ABC 练习题

· · 题解

怎么刻画序列是否合法?首先注意到:第一个 \texttt{A} 一定要分派给首个 \texttt{ABC},否则均可以调整如下:

\texttt{\color{red}CAB\color{blue}ABC} \to \texttt{\color{red}C\color{blue}AB\color{red}AB\color{blue}C} \\ \texttt{\color{red}CA\color{blue}AB\color{red}B\color{blue}C} \to \texttt{\color{red}C\color{blue}A\color{red}A\color{blue}B\color{red}B\color{blue}C}

我们据此可以分配所有 \texttt{ABC} 的首字母,同理所有 \texttt{BCA}、\texttt{CAB} 的首字母也可以分配。

第二个字母、第三个字母也可以贪心分配,枚举 x,y 就做到了 O(n^3) 判断是否合法。需要注意的是,由于倒序考虑时,取最后出现的某个字符最优,所以取第二个字母的时候,如果取的字母与之前取的首字母冲突了,那么可以直接判定不合法。

据此,对于固定的 x, y,我们的限制是(以 \texttt{ABC} 导出的限制为例,\texttt{BCA}/\texttt{CAB} 同理):

考察有关 \texttt{A} 和 \texttt{B} 的限制,那么限制恰好拼成:

对 y 的限制是不小于某个数,对 x, n - x - y 同理。合法的 (x, y) 对画到平面上是一个等腰直角三角形。

据此,把合法串挂到最小的 (x, y) 处统计是正确的,复杂度 O(n^5)。

#include <bits/stdc++.h>
#define ll long long
#define uint unsigned int
using namespace std;
inline ll Read() {
    int sig = 1; ll num = 0; char c = getchar();
    while(!isdigit(c)) { if(c == '-') sig = -1; c = getchar(); }
    while(isdigit(c)) num = (num << 3) + (num << 1) + (c ^ 48), c = getchar();
    return num * sig;
}
void Write(uint x) {
    if(x > 9) Write(x / 10);
    putchar((x % 10) ^ 48);
}
const int N = 65;
int n, a[N * 3];
uint f[2][2][N][N][N];
bool b[2][N];
void Solve() {
    int i, j, k, x, y; n = Read();
    char c = getchar();
    while(!isdigit(c)) c = getchar();
    for(i = 0; i <= n; i++) b[0][i] = c - '0', c = getchar();
    while(!isdigit(c)) c = getchar();
    for(i = 0; i <= n; i++) b[1][i] = c - '0', c = getchar();
    while(!isgraph(c)) c = getchar();
    for(i = 1; i <= n * 3; i++) a[i] = (c == '?' ? -1 : int(c - 'A')), c = getchar();
    if(n != 60) { printf("-1\n"); return ; }
    int px = -1; uint ans = 0;
    for(x = 0; x <= n; x++) if(b[0][x]) {
        int py = -1;
        for(y = 0; y <= n; y++) if(b[1][y]) {
            f[0][0][0][0][0] = 1;
            for(i = 0; i <= n; i++) for(j = 0; j <= n; j++) for(k = 0; k <= n; k++) {
                int t = i + j + k + 1;
                if(!f[0][0][i][j][k] && !f[0][1][i][j][k] && !f[1][0][i][j][k] && !f[1][1][i][j][k]) continue;
                if((a[t] == -1 || a[t] == 0) && i < n) {
                    if(i - k < px) {
                        f[0][0][i + 1][j][k] += f[0][0][i][j][k];
                        f[0][1][i + 1][j][k] += f[0][1][i][j][k];
                        f[1][0][i + 1][j][k] += f[1][0][i][j][k];
                        f[1][1][i + 1][j][k] += f[1][1][i][j][k];
                    }
                    else if(i - k < x) {
                        f[1][0][i + 1][j][k] += f[0][0][i][j][k] + f[1][0][i][j][k];
                        f[1][1][i + 1][j][k] += f[0][1][i][j][k] + f[1][1][i][j][k];
                    }
                }
                if((a[t] == -1 || a[t] == 1) && j < n) {
                    if(j - i < py) {
                        f[0][0][i][j + 1][k] += f[0][0][i][j][k];
                        f[0][1][i][j + 1][k] += f[0][1][i][j][k];
                        f[1][0][i][j + 1][k] += f[1][0][i][j][k];
                        f[1][1][i][j + 1][k] += f[1][1][i][j][k];
                    }
                    else if(j - i < y) {
                        f[0][1][i][j + 1][k] += f[0][0][i][j][k] + f[0][1][i][j][k];
                        f[1][1][i][j + 1][k] += f[1][0][i][j][k] + f[1][1][i][j][k];
                    }
                }
                if((a[t] == -1 || a[t] == 2) && k < n) {
                    if(n - (k - j) > x + y) {
                        f[0][0][i][j][k + 1] += f[0][0][i][j][k];
                        f[0][1][i][j][k + 1] += f[0][1][i][j][k];
                        f[1][0][i][j][k + 1] += f[1][0][i][j][k];
                        f[1][1][i][j][k + 1] += f[1][1][i][j][k];
                    }
                }
                if(i == n && j == n && k == n) ans += f[1][1][n][n][n];
                f[0][0][i][j][k] = f[0][1][i][j][k] = f[1][0][i][j][k] = f[1][1][i][j][k] = 0;
            }
            py = y;
        }
        px = x;
    }
    Write(ans), putchar('\n');
}
int main() {
    int T = Read(), id = Read();
    while(T--) Solve();
}
// y >= B - A
// x >= A - C
// n - (C - B) >= x + y