跳到正文

题解

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

_Z_F_R_约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年9月21日
收录
2026年9月25日
更新
2026年11月10日
篇幅
约 8 分钟
题目
P12472

版本

  1. v2当前
  2. v1

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

BCAABC→BCAABCCABABC→CABABCCAABBC→CAABBC\texttt{\color{red}BCA\color{blue}ABC} \to \texttt{\color{red}BC\color{blue}A\color{red}A\color{blue}BC} \\ \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}

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

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

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

  • 从左往右数第 ii 个 A\texttt{A} 要在从左往右数第 y+iy + i 个 B\texttt{B} 之前。
  • 从左往右数第 y+iy + i 个 B\texttt{B} 要在从左往右数第 n−i+1n - i + 1 个 C\texttt{C} 之前。

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

  • 对 i∈[1,n−y+1]i \in [1, n - y + 1],从左往右数第 ii 个 A\texttt{A} 要在从左往右数第 y+iy + i 个 B\texttt{B} 之前。

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

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

C++
#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
原文