跳到正文

题解

题解:P8227 「Wdoi-5」建立与摧毁的结界

qiuby123456约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年8月22日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 6 分钟
获赞
1
题目
P8227

版本

  1. v1当前

首先为了更方便地描述,我们约定:

  • 用大写字母表示一个合法括号序列;
  • “展平括号序列 SS”表示将括号序列 SS 变成平铺括号序列,f(S)f(S) 表示展开 SS 的最小操作数;
  • “嵌套括号序列 SS”表示将括号序列 SS 变成嵌套括号序列,d(S)d(S) 表示嵌套 SS 的最小操作数。

容易发现,f(())=d(())=0f(\textcolor{red}{\texttt{()}})=d(\textcolor{red}{\texttt{()}})=0。

要展平 B=(A)B=\texttt{(}A\texttt{)},那么就要嵌套 AA,才能一步到位展平 BB。即:f(B)=d(A)+1f(B)=d(A)+1。

要嵌套 B=A1A2A3⋯AxB=A_1A_2A_3\cdots A_x,其中 ∀i:1≤i≤x\forall i:1\le i\le x,Ai=(S)A_i=\texttt{(}S\texttt{)} 或者 Ai=()A_i=\texttt{()},那么就要依次进行以下操作:∀i:1≤i≤x\forall i:1\le i\le x,展平 AiA_i,这样才能直接嵌套 BB。即:d(B)=(∑i=1xf(Ai))+1d(B)=(\sum_{i=1}^x f(A_i))+1。

我们发现展平操作和嵌套操作互为逆操作,也就是说如果 AA 通过一系列操作变成 BB,那么 BB 一定能通过相反顺序的逆操作变成 AA。

我们考虑下面的情形(其中连续的一段 .\texttt{.} 表示一个合法括号序列):

A=(...)(....)(.)(...)(..)(....)B=(...)(...)(..)(.......)(....)A=\texttt{(\textcolor{red}{...})\textcolor{orange}{(....)(.)}\textcolor{blue}{(...)(..)}(\textcolor{green}{....})}\\B=\texttt{(\textcolor{red}{...})\textcolor{orange}{(...)(..)}\textcolor{blue}{(.......)}(\textcolor{green}{....})}

那么我们发现,如果存在两段合法括号序列 CC 是 AA 的子串、DD 是 BB 的子串(比如上面 AA 和 BB 相同颜色的彩色子串),满足 CC 在 AA 中的位置和 DD 在 BB 中的位置相等,并且不存在 SS 是 CC 的子串、TT 是 DD 的子串满足上述条件,那么记这个条件为条件 pp,从 CC 转换到 DD 一定需要有展平 CC 的操作才能转换成 DD。这样我们只需要分别展平 CC 和 DD 即可,操作数为 f(C)+f(D)f(C)+f(D)。最终的总操作数就是对于所有满足条件 pp 的子串 CC 和 DD,∑(f(C)+f(D))\sum (f(C)+f(D))。

对于输入的括号序列 AA 和 BB,每个字符只会被处理常数次,所以时间复杂度为 O(n)\mathcal O(n)。

以下是我的 AC 代码。实际上我写了三份代码,但是只有下面这一个能 AC,我也不知道为什么。

C++
#include <cstdio>
//next1 和 next2 分别存储序列 A 和 B 每个左括号对应的右括号的下标
//我的下标从 0 开始
//stack 是栈,为了匹配括号而设置,t 是栈顶
int next1[1000001], next2[1000001], stack[500001], n, c, t;
int f(const int*, int, int);
int d(const int *next, int l, int r){
	if (l + 2 == r){//() 序列无需嵌套
		//因为 () 也是平铺括号序列,但是我的 f 函数调用 d 时默认是需要再进行一次嵌套操作的
		//为了抵消这次操作,我在这返回 -1
		return -1;
	}
	while (l + 2 < r && next[l] + 1 == r){//(A) 形式的序列
		++l;
		--r;
	}
	return l + 2 != r ? f(next, l, r) + 1/*不是嵌套括号序列*/ : 0/*已经是嵌套括号序列但不是平铺括号序列*/;
}
int f(const int *next, int l, int r){
	if (l + 2 == r){//() 序列无需展平
		return 0;
	}
	int sum = 0, j;
	for (int i = l; i < r; ){
		j = d(next, i, next[i] + 1);//分别展平
		sum += j < 0 ? 0 : j + 1;
		i = next[i] + 1;
	}
	return sum;
}
//work 处理分割满足条件 p 的括号序列
int work(int l, int r){
	if (l == r){//序列为空
		return 0;
	}
	int i = l, sum = 0;
	while (i < r){
		if (next1[i] == next2[i]){//处理到 (A) 转换成 (B) 的形式,向内递归一层
			sum += work(i + 1, next1[i]);
			i = next1[i] + 1;
		}
		else{
			//处理满足条件 p、子串第一个左括号从原来的 i 开始的子串
			//我采用的是分别展开的方式
			int j = next2[i] + 1;
			sum += f(next1, i, next1[i] + 1) + f(next2, i, j);
			i = next1[i] + 1;
			while (i != j){
				while (i < j){
					sum += f(next1, i, next1[i] + 1);
					i = next1[i] + 1;
				}
				while (i > j){
					sum += f(next2, j, next2[j] + 1);
					j = next2[j] + 1;
				}
			}
		}
	}
	return sum;
}
int main(){
	scanf("%d", &n);
	getchar();
	for (int i = 0; i < n; ++i){
		(c = getchar() & 1) ? (next1[stack[--t]] = i) : (stack[t++] = i);
	}
	getchar();
	for (int i = 0; i < n; ++i){
		(c = getchar() & 1) ? (next2[stack[--t]] = i) : (stack[t++] = i);
	}
	printf("%d", work(0, n));
	return 0;
}

题外话:有没有大佬帮忙调一下这三份代码,求求了,解决了问题必关。

原文